80,348
社区成员
发帖
与我相关
我的任务
分享
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//发送消息
projectionManager= (MediaProjectionManager)getApplication().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
Intent intent =projectionManager.createScreenCaptureIntent();
startActivityForResult(intent,REQUEST_MEDIA_PROJECTION);
Toast.makeText(MainActivity.this.getApplication(), "开始", Toast.LENGTH_SHORT).show();
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_MEDIA_PROJECTION) {
if (resultCode != Activity.RESULT_OK) {
return;
} else if (data != null && resultCode != 0) {
Toast.makeText(MainActivity.this.getApplication(), "截图已存在", Toast.LENGTH_SHORT).show();
String nameImage = "/sdcard/1.png";
//WindowManager对象用于获取屏幕尺寸
WindowManager windowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
windowheight = windowManager.getDefaultDisplay().getHeight();
windowWidth = windowManager.getDefaultDisplay().getWidth();
//获取屏幕dpi
screenDpi = new DisplayMetrics().densityDpi;
//获取MediaProjection
mediaProjection = projectionManager
.getMediaProjection(REQUEST_MEDIA_PROJECTION, data);
//通过windowWidth,windowheight创建新建一个ImageReader对象
ImageReader imageReader = ImageReader.newInstance(windowWidth, windowheight, 0x1, 2);
//创建VirtualDisplay对象
if (imageReader.getSurface() == null)
Log.i("++-->>", "000");
virtualDisplay = mediaProjection.createVirtualDisplay("屏幕捕获", windowWidth, windowheight, screenDpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, imageReader.getSurface(), null, null);
//得到Image对象
Image image = imageReader.acquireLatestImage();
int width = image.getWidth();
int height = image.getHeight();
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 = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height);
image.close();
if (bitmap != null) {
try {
File fileImage = new File(nameImage);
if (!fileImage.exists()) {
fileImage.createNewFile();
Toast.makeText(MainActivity.this.getApplication(), "截图已存在", Toast.LENGTH_SHORT).show();
}
FileOutputStream out = new FileOutputStream(fileImage);
if (out != null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(fileImage);
media.setData(contentUri);
this.sendBroadcast(media);
Toast.makeText(MainActivity.this.getApplication(), "截图成功", Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Toast.makeText(MainActivity.this.getApplication(), String.valueOf(requestCode), Toast.LENGTH_SHORT).show();
}
}
}