如何在android 10和所有android版本中使用媒体投影拍摄屏幕?
当我想在安卓10上截图时,我的api目标是29,但它崩溃了 我遇到的另一个问题是,当我想在安卓10中的屏幕截图第一次显示黑屏时,但在那之后,它就可以毫无问题地运行了 这是我的代码:如何在android 10和所有android版本中使用媒体投影拍摄屏幕?,android,screenshot,android-mediaprojection,mediaprojection,Android,Screenshot,Android Mediaprojection,Mediaprojection,当我想在安卓10上截图时,我的api目标是29,但它崩溃了 我遇到的另一个问题是,当我想在安卓10中的屏幕截图第一次显示黑屏时,但在那之后,它就可以毫无问题地运行了 这是我的代码: public class ScreenshotManager { private FileOutputStream fos = null; Activity context; MediaProjection mediaProjection; public static Intent screenshotPermiss
public class ScreenshotManager {
private FileOutputStream fos = null;
Activity context;
MediaProjection mediaProjection;
public static Intent screenshotPermission = null;
MediaProjectionManager mediaProjectionManager;
private static final String SCREENCAP_NAME = "screencap";
private static final int VIRTUAL_DISPLAY_FLAGS = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
public static final ScreenshotManager INSTANCE = new ScreenshotManager();
private Intent mIntent;
int width,height;
public void requestScreenshotPermission(@NonNull Activity activity, int requestId) {
this.context=activity;
mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
activity.startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), requestId);
}
public void onActivityResult(int resultCode, Intent data) {
if (Activity.RESULT_OK == resultCode && data!=null) {
if(screenshotPermission!=null){
mIntent=screenshotPermission;
}else{
mIntent=data;
screenshotPermission=data;
}
}
else {
mIntent=null;
}
}
@UiThread
public boolean takeScreenshot(@NonNull Activity context) {
this.context=context;
if (mIntent == null)
return false;
try{
final MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mediaProjection = mediaProjectionManager.getMediaProjection(Activity.RESULT_OK, mIntent);
if (mediaProjection == null)
return false;
final int density = context.getResources().getDisplayMetrics().densityDpi;
final Point windowSize = new Point();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getRealSize(windowSize);
final ImageReader imageReader = ImageReader.newInstance(windowSize.x, windowSize.y, PixelFormat.RGBA_8888, 1);
final VirtualDisplay virtualDisplay = mediaProjection.createVirtualDisplay(SCREENCAP_NAME, width, height, density, VIRTUAL_DISPLAY_FLAGS, imageReader.getSurface(), null, null);
Handler handler=new Handler();
imageReader.setOnImageAvailableListener(new ImageAvailableListener(), handler);
mediaProjection.registerCallback(new MediaProjection.Callback() {
@Override
public void onStop() {
super.onStop();
if (virtualDisplay != null)
virtualDisplay.release();
imageReader.setOnImageAvailableListener(null, null);
mediaProjection.unregisterCallback(this);
}
}, null);
}catch (Exception e){
}
return true;
}
private class ImageAvailableListener implements ImageReader.OnImageAvailableListener {
/**
* Callback that is called when a new image is available from ImageReader.
*
* @param reader the ImageReader the callback is associated with.
* @see ImageReader
* @see Image
*/
@Override
public void onImageAvailable(ImageReader reader) {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
MediaPlayer mPlayer = MediaPlayer.create(context,R.raw.shutter_sound);
mPlayer.start();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Image image = null;
Bitmap bitmap = null;
try {
image = reader.acquireLatestImage();
if (image != null) {
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/MyScreenshots");
if (!folder.exists())
folder.mkdirs();
String sPath = Environment.getExternalStorageDirectory().toString() + "/MyScreenshots";
File file = new File(sPath, "pic_" + now + ".jpg");
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride(), rowStride = planes[0].getRowStride(), rowPadding = rowStride - pixelStride * width;
bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
fos = new FileOutputStream(sPath + "/pic_" + now + ".jpg");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
mediaProjection.stop();
Utils.ScreenShotAlert(context, file);
}
} catch (Exception e) {
if (bitmap != null)
bitmap.recycle();
e.printStackTrace();
}
if (image != null)
image.close();
reader.close();
}
}, 500);
}
}
}
我试着用这项服务解决问题,但没有解决
如何解决这个问题