我是参考的这个一种全新的截屏方式;
但是在截屏时要获取[屏幕截取]的权限,要用到startActivityForResult
MediaProjectionManager projectionManager = (MediaProjectionManager) getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(projectionManager.createScreenCaptureIntent(),1);
但是每次就会出现如下的情况
下面是我的MainActivity
package com.example.ttt;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Point;
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.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private MediaProjectionManager projectionManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.click).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
projectionManager = (MediaProjectionManager) getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
startActivityForResult(projectionManager.createScreenCaptureIntent(),1);
handleScreenShotIntent();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1){
((MyApplication) getApplication()).setResult(resultCode);
((MyApplication) getApplication()).setIntent(data);
}
}
private void handleScreenShotIntent() {
try {
onScreenshotTaskBegan();
final MediaProjection mProjection = projectionManager.getMediaProjection(
((MyApplication) getApplication()).getResult(), ((MyApplication) getApplication()).getIntent());
Point size = Utils.getScreenSize(this);
final int mWidth = size.x;
final int mHeight = size.y;
final ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
final VirtualDisplay display = mProjection.createVirtualDisplay("screen-mirror", mWidth, mHeight,
DisplayMetrics.DENSITY_MEDIUM, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION,
mImageReader.getSurface(), null, null);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@SuppressLint("NewApi")
@Override
public void onImageAvailable(ImageReader mImageReader) {
Image image = null;
try {
image = mImageReader.acquireLatestImage();
if (image != null) {
final Image.Plane[] planes = image.getPlanes();
if (planes.length > 0) {
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
// create bitmap
Bitmap bmp = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight,
Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(buffer);
Bitmap croppedBitmap = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight);
saveBitmap(croppedBitmap);// 保存图片
if (croppedBitmap != null) {
croppedBitmap.recycle();
}
if (bmp != null) {
bmp.recycle();
}
}
}
} catch (Exception e) {
e.printStackTrace();
// showErrorNotification(Utils.getStackTrace(e));
} finally {
if (image != null) {
image.close();
}
if (mImageReader != null) {
mImageReader.close();
}
if (display != null) {
display.release();
}
mImageReader.setOnImageAvailableListener(null, null);
mProjection.stop();
onScreenshotTaskOver();
}
}
}, getBackgroundHandler());
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveBitmap(Bitmap bmp) throws IOException {
File childFolder = SettingHelper.getOutputFoler();
File imageFile = new File(childFolder.getAbsolutePath() + "/" + SettingHelper.getOutputScreenshotFileName());
OutputStream fOut = new FileOutputStream(imageFile);
bmp.compress(Bitmap.CompressFormat.JPEG, 80, fOut);// 将bg输出至文件
fOut.flush();
fOut.close(); // do not forget to close the stream
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(imageFile)));
}
static AtomicInteger oneScreenshot = new AtomicInteger(0);
private boolean isAlreadyOneScreenshotTask() {
return oneScreenshot.get() == 1;
}
private synchronized void onScreenshotTaskBegan() {
oneScreenshot.set(1);
}
private synchronized void onScreenshotTaskOver() {
oneScreenshot.set(0);
}
// 在后台线程里保存文件
Handler backgroundHandler;
private Handler getBackgroundHandler() {
if (backgroundHandler == null) {
HandlerThread backgroundThread = new HandlerThread("easyscreenshot",
android.os.Process.THREAD_PRIORITY_BACKGROUND);
backgroundThread.start();
backgroundHandler = new Handler(backgroundThread.getLooper());
}
return backgroundHandler;
}
}
MyApplication:
package com.example.ttt;
import android.app.Application;
import android.content.Intent;
import android.media.projection.MediaProjectionManager;
public class MyApplication extends Application {
private int result;
private Intent intent;
private MediaProjectionManager mMediaProjectionManager;
public int getResult() {
return result;
}
public Intent getIntent() {
return intent;
}
public MediaProjectionManager getMediaProjectionManager() {
return mMediaProjectionManager;
}
public void setResult(int result1) {
this.result = result1;
}
public void setIntent(Intent intent1) {
this.intent = intent1;
}
public void setMediaProjectionManager(MediaProjectionManager mMediaProjectionManager) {
this.mMediaProjectionManager = mMediaProjectionManager;
}
}