我需要长时间获取屏幕截图,但这会使系统崩溃。
这是我的MainActivity
大约220次,系统将崩溃。
我猜可能是内存泄漏引起的,但我找不到。
电话:oneplus 7 pro
OS:氧气OS(Android 9)
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1050;
private String TAG = "Service";
private MainApplication mainApplication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainApplication = (MainApplication) getApplication();
if (MainApplication.sceenWidth == 0 || MainApplication.sceenHeight == 0) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(dm);
MainApplication.sceenHeight = Math.min(dm.heightPixels, dm.widthPixels);
MainApplication.sceenWidth = Math.max(dm.heightPixels, dm.widthPixels);
MainApplication.dpi = dm.densityDpi;
}
// mainApplication.init(this);
ScreenCaptureUtilByMediaPro.mProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
// init
startActivityForResult(ScreenCaptureUtilByMediaPro.mProjectionManager.createScreenCaptureIntent(), REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (RESULT_OK == resultCode && REQUEST_CODE == requestCode) {
ScreenCaptureUtilByMediaPro.data = data;
ScreenCaptureUtilByMediaPro.resultCode = resultCode;
ScreenCaptureUtilByMediaPro.init();
int i = 0;
while (true) {
Bitmap screenCap = ScreenCaptureUtilByMediaPro.getScreenCap();
System.out.println(screenCap);
System.out.println(i);
i++;
sleep(2000);
}
}
}
}
和其他班级
public class ScreenCaptureUtilByMediaPro {
public static MediaProjectionManager mProjectionManager;
public static Intent data;
public static int resultCode;
private static MediaProjection sMediaProjection;
private static ImageReader mImageReader;
private static VirtualDisplay mVirtualDisplay;
private static Bitmap bitmapCache;
public static void init() {
sMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
//start capture reader
mImageReader = ImageReader.newInstance(MainApplication.sceenWidth, MainApplication.sceenHeight,
PixelFormat.RGBA_8888, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay(
"ScreenShot",
MainApplication.sceenWidth,
MainApplication.sceenHeight,
MainApplication.dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC,
mImageReader.getSurface(),
null,
null);
}
/**
*
*
* @return
*/
@Deprecated
public static Bitmap getScreenCap() {
Bitmap bitmap;
android.media.Image image;
do {
image = mImageReader.acquireLatestImage();
if (image == null && bitmapCache != null) {
return bitmapCache;
}
} while (image == null);
bitmap = covetBitmap(image);
if (bitmapCache != null) {
bitmapCache.recycle();
bitmapCache = null;
}
bitmapCache = bitmap;
return bitmap;
}
public static Bitmap covetBitmap(android.media.Image image) {
int width = image.getWidth();
int height = image.getHeight();
final android.media.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 bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
image.close();
return bitmap;
}
}