使用媒体投影Android Studio截屏的问题 [英] Problem with taking screenshots with the media projection android studio
本文介绍了使用媒体投影Android Studio截屏的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 我有这段代码可以制作屏幕截图.在android studio的虚拟设备中,此代码可以完美运行,但是在我自己的设备Samsung(带有android 8.0)中,当我执行apk安装程序时,该程序仅会截取一些屏幕截图.例如,我按下按钮拍摄10张屏幕截图,但是该程序仅为我保存2张,其余的都不存在.现在我不知道出了什么问题.
@SuppressLint("WrongConstant")
@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;
}
推荐答案
我不知道您是否已经解决了问题,但这是我的解决方案:该方法接收4个参数,最后一个是照片数量
I don't know if you've already solved your problem, but here's my solution: The method receives 4 parameters and the last one is the number of photos
在此处输入代码
ImageReader.newInstance(宽度:整数,高度:整数,格式:整数,最大图像:整数)
enter code here
ImageReader.newInstance(width: Int, height: Int, format: Int, maxImages: Int)
如果看起来不错,请在最后一个参数中发送照片数量2
If you look good you are sending it in the last parameter the number of photos 2
在此处输入代码
ImageReader.newInstance(mWidth,mHeight,PixelFormat.RGBA_8888,2);
enter code here
ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
这篇关于使用媒体投影Android Studio截屏的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文