【问题标题】:Problem with taking screenshots with the media projection android studio使用媒体投影android studio截屏的问题
【发布时间】:2020-09-30 00:04:35
【问题描述】:
  1. 我有这个代码来制作屏幕截图。在 android studio 的虚拟设备中,此代码运行良好,但在我自己的设备三星(使用 android 8.0)中,当我执行 apk 安装程序时,该程序只截取一些屏幕截图。示例 我按下按钮截取 10 个屏幕截图,但程序只保存了我 2 个,其余的都不存在。现在我不知道出了什么问题。

@SuppressLint("WrongConstant")

private void screenshot() {

    Point size = new Point();
    displayy.getSize(size);
    mWidth = size.x;
    mHeight = size.y;
    displayy.getMetrics(metrics);
    int mDensity = metrics.densityDpi;
    Handler handler=new Handler();


    mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        displayyy = mMediaProjection.createVirtualDisplay(DISPLAY_NAME, mWidth, mHeight, mDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, handler);
    }
    mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
        int onImageCount = 0;
        
        @Override
        public void onImageAvailable(ImageReader reader) {

            Image image = null;
            FileOutputStream fos = null;
            Bitmap bitmap = null;

            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    image = reader.acquireLatestImage();
                }
                if (image != null) {
                    Image.Plane[] planes = new Image.Plane[0];
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                        planes = image.getPlanes();
                    }
                    ByteBuffer buffer = planes[0].getBuffer();
                    int pixelStride = planes[0].getPixelStride();
                    int rowStride = planes[0].getRowStride();
                    int rowPadding = rowStride - pixelStride * mWidth;

                    bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
                    
                    bitmap.copyPixelsFromBuffer(buffer);
                    
                    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy_HH:mm:ss");
                    String formattedDate = df.format(Calendar.getInstance().getTime()).trim();
                    String finalDate = formattedDate.replace(":", "-");

                    String imgName = "/imagen"+ "_" + finalDate + ".jpg";
                    String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                            + "/example";
                    File dir = new File(dirPath);
                    if(!dir.exists()){
                        dir.mkdirs();
                    }

                    String mPath = dirPath + imgName;
                    File imageFile = new File(mPath);

                    fos = new FileOutputStream(imageFile);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    Log.e(TAG, "captured image: " );
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                }

                if (bitmap != null) {
                    bitmap.recycle();
                }

                if (image != null) {
                    image.close();
                }
            }
        }
    }, handler);
    
    mMediaProjection.stop();
    mMediaProjection=null;
}

【问题讨论】:

  • 然后我重启手机,图片不存在,这不是解决办法
  • 我有答案很遗憾有人帮忙

标签: java android performance android-studio screenshot


【解决方案1】:

我不知道您是否已经解决了您的问题,但这是我的解决方案: 该方法接收4个参数,最后一个是照片数量

enter code hereImageReader.newInstance(width: Int, height: Int, format: Int, maxImages: Int)

如果你看起来不错你在最后一个参数中发送照片的数量 2

enter code hereImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);

enter link description here

【讨论】:

  • 感谢您的回答,但这不是解决方案,解决方案是按照我需要将一种方法放在其他方法之前的方法的顺序
猜你喜欢
  • 2019-11-18
  • 1970-01-01
  • 2022-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
相关资源
最近更新 更多