如何在android 10和所有android版本中使用媒体投影进行屏幕截图?

问题描述

当我想在android 10中截屏并且我的api目标是29但它崩溃时,

我遇到的另一个问题是,当我想在android 10中截图时第一次显示黑屏,但之后它就没有问题了

这是我的代码

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,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);
    }

}
}

我尝试将服务用于地面,但解决不了

如何解决此问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...