android 两种截屏方式
2017-08-09 13:43
253 查看
截屏的时候会有卡顿,暂时未解决。
第一种(这种截图只会截屏应用内,状态栏是不会截到的):
调用方法
在调用之前需要判断权限
接下来是截图的具体方法:
第二种方法(这个只支持5.0的系统以上,截取整个视图):
调用方法,需要判断api版本
接下来是截图的方法
这种方法需要手动允许截屏的权限。
第一种(这种截图只会截屏应用内,状态栏是不会截到的):
调用方法
if (ContextCompat.checkSelfPermission(ShareCodeActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(ShareCodeActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 54); Log.i("-->", "权限申请"); } else { screenshot(); }
在调用之前需要判断权限
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); doNext(requestCode, grantResults); } private void doNext(int requestCode, int[] grantResults) { if (requestCode == 54) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { screenshot(); } else { new AlertDialog.Builder(this) .setTitle("权限申请") .setMessage("在设置-应用-(应用名称)-权限中启动存储权限,以正常使用此功能") .setPositiveButton("去设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); startActivity(intent); } }) .setNegativeButton("取消", null) .show(); } } }
接下来是截图的具体方法:
/** * 获取屏幕 */ private void screenshot() { // 获取屏幕 View dView = getWindow().getDecorView(); dView.setDrawingCacheEnabled(true); dView.buildDrawingCache(); Bitmap bmp = dView.getDrawingCache(); File file = PathGetUtils.getFile(this); if (bmp != null) { try { Log.i("-->", "路径:" + file.getPath() + " " + file.getAbsolutePath()); if (file.exists()) { file.delete(); } FileOutputStream os = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); String mUri = MediaStore.Images.Media.insertImage(getContentResolver(), file.getPath(), "ck", null); if (mUri != null) { Toast.makeText(ShareCodeActivity.this, "截图成功", Toast.LENGTH_SHORT).show(); // 最后通知图库更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(mUri)); intent.setData(uri); sendBroadcast(intent); } else { Toast.makeText(ShareCodeActivity.this, "截图失败", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { } } }其中获取的bitmap就是截屏图片.
第二种方法(这个只支持5.0的系统以上,截取整个视图):
调用方法,需要判断api版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ mMpMngr = (MediaProjectionManager) getApplicationContext().getSystemService(Context.MEDIA_PROJECTION_SERVICE); if(NoDoubleClick.isFastClick()){ startIntent(); } }else { Toast.makeText(this, "您的手机系统暂不支持截屏功能", Toast.LENGTH_SHORT).show(); }
接下来是截图的方法
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //3、通过onActivityResult()获取授权结果。 if (requestCode == REQUEST_MEDIA_PROJECTION && resultCode == RESULT_OK) { MyApp.mResultCode = resultCode; MyApp.mResultIntent = data; MyApp.mMpmngr = mMpMngr; newPrintScreen(); } } private void startIntent() { if (MyApp.mResultIntent != null && MyApp.mResultCode != 0) { //已授权 newPrintScreen(); } else{ //2、调用MediaProjectionManager.createScreenCaptureIntent()后,会弹出一个dialog询问用户是否授权应用捕捉屏幕 startActivityForResult(mMpMngr.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);//未授权 } } private void newPrintScreen(){ wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); DisplayMetrics metric = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metric); windowWidth = metric.widthPixels; windowHeight = metric.heightPixels; screenDensity = metric.densityDpi; mImageReader = ImageReader.newInstance(windowWidth, windowHeight, 0x1, 2); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Log.e(TAG, "start startVirtual"); startVirtual(); } }, 000); handler.postDelayed(new Runnable() { @Override public void run() { Log.e(TAG, "start startCapture"); startCapture(); } }, 500); handler.postDelayed(new Runnable() { @Override public void run() { Log.e(TAG, "start stopVirtual"); stopVirtual(); } }, 1000); } private void stopVirtual() { if (mVirtualDisplay != null) { mVirtualDisplay.release(); mVirtualDisplay = null; } } private void startCapture() { mImageName = System.currentTimeMillis() + ".png"; Log.e(TAG, "image name is : " + mImageName); Image image = mImageReader.acquireLatestImage(); 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 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) { Log.e(TAG, "bitmap create success "); try { File fileFolder = new File(mImagePath); if (!fileFolder.exists()) fileFolder.mkdirs(); File file = new File(mImagePath, mImageName); if (!file.exists()) { Log.e(TAG, "file create success "); file.createNewFile(); } FileOutputStream out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); out.flush(); out.close(); Log.e(TAG, "file save success "); s 4000 hareDialog(bitmap); if (mMpj != null) { mMpj.stop(); mMpj = null; } } catch (IOException e) { Log.e(TAG, e.toString()); e.printStackTrace(); } } } private void startVirtual() { if (mMpj == null) mMpj = MyApp.mMpmngr.getMediaProjection(MyApp.mResultCode, MyApp.mResultIntent); mVirtualDisplay = mMpj.createVirtualDisplay("capture_screen", windowWidth, windowHeight, screenDensity, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null); }
这种方法需要手动允许截屏的权限。
相关文章推荐
- android绘制圆形图片的两种方式示例
- Android 蓝牙打印小票与WiFi打印小票两种打印方式的实现(带有图片和二维码)
- Android DES加解密的两种实现方式 - Java DES与Android DES不兼容问题
- Android中自定义Dialog常用的两种方式
- Android动画Animation的两种加载执行方式
- Android使用Intent传递对象的两种方式
- android中两种方式打开网页
- Android 编程下两种方式注册广播的区别
- Android Studio签名打包的两种方式
- Android基础--拨打电话两种方式
- Android BroadcastReceiver两种注册方式区别
- android ,关于广播接收者的两种不同方式的用法及区别
- Android-两种方式实现走马灯效果
- android之fragment的两种使用方式(add和replace方式切换)
- Android开发之使用sqlite3工具操作数据库的两种方式
- Android沉浸式状态栏的两种实现方式
- Android之fragment的两种创建方式
- Android Service两种启动方式详解(总结版)
- android获取Bitmap的两种方式
- Android 启动 Service(startservice和bindservice) 两种方式的区别