17 | * There is totally transparent activity,only has a record permission dialog. 18 | * If you want to screenshot on other applications,might you need to use this activity to take screenshot. 19 | */ 20 | public class ScreenShotActivity extends Activity { 21 | 22 | public static final String KEY_PATH = "path"; 23 | public static final String KEY_DELAY = "delay_time"; 24 | 25 | public static final int REQUEST_MEDIA_PROJECTION = 0x2304; 26 | public static final String ACTION_SHOTER = "androidyuan.shooter"; 27 | 28 | private String savedPath; 29 | private long delay; 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | 34 | // setTheme(android.R.style.Theme_Dialog);//this line cause a problem, activity background wasn't transparent but black. 35 | super.onCreate(savedInstanceState); 36 | 37 | //here is a transparent activity,and previous activity will not be called Activity#onPause(). 38 | requestWindowFeature(Window.FEATURE_NO_TITLE); 39 | getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 40 | getWindow().setDimAmount(0f); 41 | savedPath = getIntent().getStringExtra(KEY_PATH); 42 | delay = getIntent().getLongExtra(KEY_DELAY,0); 43 | requestScreenShotPermission(); 44 | } 45 | 46 | public static Intent createIntent(Context context, String path,long delay) { 47 | Intent intent = new Intent(context, ScreenShotActivity.class); 48 | intent.putExtra(KEY_PATH, path); 49 | intent.putExtra(KEY_DELAY, delay); 50 | return intent; 51 | } 52 | 53 | 54 | public void requestScreenShotPermission() { 55 | if (Build.VERSION.SDK_INT >= 21) { 56 | startActivityForResult(createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION); 57 | } 58 | } 59 | 60 | private Intent createScreenCaptureIntent() { 61 | //here used media_projection instead of Context.MEDIA_PROJECTION_SERVICE to make it successfully build on low api. 62 | return ((MediaProjectionManager) getSystemService("media_projection")).createScreenCaptureIntent(); 63 | } 64 | 65 | 66 | protected void onActivityResult(int requestCode, final int resultCode, final Intent data) { 67 | super.onActivityResult(requestCode, resultCode, data); 68 | switch (requestCode) { 69 | case REQUEST_MEDIA_PROJECTION: { 70 | if (resultCode == RESULT_OK && data != null) { 71 | 72 | getWindow().getDecorView().postDelayed(new Runnable() { 73 | @Override 74 | public void run() { 75 | Shooter shooter = new Shooter(ScreenShotActivity.this, resultCode, data); 76 | shooter.startScreenShot(savedPath, new Shooter.OnShotListener() { 77 | @Override 78 | public void onFinish(String path) { 79 | Intent intent = new Intent(); 80 | intent.setData(Uri.parse(path)); 81 | setResult(RESULT_OK, intent); 82 | finish(); // don't forget finish activity 83 | } 84 | 85 | @Override 86 | public void onError() { 87 | setResult(RESULT_CANCELED); 88 | finish(); 89 | } 90 | }); 91 | } 92 | },delay); 93 | } else if (resultCode == RESULT_CANCELED) { 94 | setResult(RESULT_CANCELED); 95 | finish(); 96 | } else { 97 | setResult(RESULT_CANCELED); 98 | finish(); 99 | } 100 | } 101 | } 102 | } 103 | 104 | 105 | } -------------------------------------------------------------------------------- /libshot/src/main/java/com/androidyuan/lib/screenshot/Shooter.java: -------------------------------------------------------------------------------- 1 | package com.androidyuan.lib.screenshot; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.res.Resources; 7 | import android.graphics.Bitmap; 8 | import android.graphics.PixelFormat; 9 | import android.hardware.display.DisplayManager; 10 | import android.hardware.display.VirtualDisplay; 11 | import android.media.Image; 12 | import android.media.ImageReader; 13 | import android.media.projection.MediaProjection; 14 | import android.media.projection.MediaProjectionManager; 15 | import android.os.AsyncTask; 16 | import android.os.Build; 17 | import android.os.Handler; 18 | import android.os.SystemClock; 19 | import android.text.TextUtils; 20 | import android.util.DisplayMetrics; 21 | import android.view.Display; 22 | import android.view.WindowManager; 23 | 24 | import java.io.File; 25 | import java.io.FileNotFoundException; 26 | import java.io.FileOutputStream; 27 | import java.io.IOException; 28 | import java.lang.ref.SoftReference; 29 | import java.nio.ByteBuffer; 30 | 31 | /** 32 | * Created by wei on 16-12-1. 33 | *
34 | * Remind:
35 | * Run this class after you got record permission.
36 | */
37 | public class Shooter {
38 |
39 | public static boolean hasPermission;
40 |
41 | private final SoftReference