I am writing simplee Screenshot App and use MediaProjection + ImageReader for this. I use some sample for resolve this task.
When i click on Capture button, i ALWAYS get "image: NULL"
in the log message.
In ImageReader.newInstance
i set Format as ImageFormat.RGB_565
. Default value was 0x1
, but when i used this value i have warning message:
Must be one of: ImageFormat.UNKNOWN, ImageFormat.RGB_565, ImageFormat.YV12, ImageFormat.Y8, ImageFormat.NV16, ImageFormat.NV21, ImageFormat.YUY2, ImageFormat.JPEG, ImageFormat.DEPTH_JPEG, ImageFormat.YUV_420_888, ImageFormat.YUV_422_888, ImageFormat.YUV_444_888, ImageFormat.FLEX_RGB_888, ImageFormat.FLEX_RGBA_8888, ImageFormat.RAW_SENSOR, ImageFormat.RAW_PRIVATE, ImageFormat.RAW10, ImageFormat.RAW12, ImageFormat.DEPTH16, ImageFormat.DEPTH_POINT_CLOUD, ImageFormat.PRIVATE, ImageFormat.HEIC
My question: why Image from ImageReader is always Null? And how to fix it correctly? Thank you
My code:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.ImageFormat;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "test_t";
private ImageReader mImageReader;
private MediaProjectionManager mMediaProjectionManager;
private Intent mCreateScreenCaptureIntent;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View mBtnCapture = findViewById(R.id.btn_capture);
mBtnCapture.setOnClickListener(this);
init();
}
@Override
protected void onDestroy() {
super.onDestroy();
tearDownMediaProjection();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mMediaProjection = mMediaProjectionManager.getMediaProjection(resultCode, data);
if (mMediaProjection != null) {
startScreenCapture();
takeCapture();
stopScreenCapture();
}
}
@Override
public void onClick(View v) {
startActivityForResult(mCreateScreenCaptureIntent, 777);
}
private void init() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
mImageReader = ImageReader.newInstance(displayMetrics.widthPixels, displayMetrics.heightPixels, ImageFormat.RGB_565, 2); // format 0x1
mMediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
mCreateScreenCaptureIntent = mMediaProjectionManager.createScreenCaptureIntent();
}
private void startScreenCapture() {
if (mMediaProjection == null)
return;
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
mVirtualDisplay = mMediaProjection.createVirtualDisplay(
"ScreenCapture",
displayMetrics.widthPixels,
displayMetrics.heightPixels,
displayMetrics.densityDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mImageReader.getSurface(), null, null);
}
private void stopScreenCapture() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
mVirtualDisplay = null;
}
private void takeCapture() {
Image image = mImageReader.acquireLatestImage();
if (image == null) {
Log.d(TAG, "image: NULL");
return;
}
int width = image.getWidth();
int height = image.getHeight();
final Image.Plane[] planes = image.getPlanes();
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * width;
Bitmap mBitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
mBitmap.copyPixelsFromBuffer(buffer);
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height);
image.close();
saveBitmapToFile(mBitmap);
}
private void saveBitmapToFile(Bitmap bitmap) {
File directory = new File(Environment.getExternalStorageDirectory(), "SCREEN_TEMP");
if (!directory.exists())
directory.mkdirs();
String name = "shot" + new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date()) + "." + Bitmap.CompressFormat.PNG.toString();
File file = new File(directory, name);
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void tearDownMediaProjection() {
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}
}