Android截屏方法

  // 截屏方法
    private void getSnapshot() {
        wView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        wView.layout(0, 0, wView.getMeasuredWidth(), wView.getMeasuredHeight());
        //开启绘图缓存
        wView.setDrawingCacheEnabled(true);
        wView.buildDrawingCache();
        //获取绘图缓存
        bitmap = wView.getDrawingCache();
        android.util.Log.d(TAG, "bitmap--" + bitmap);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        screenShotFielPath = Environment.getExternalStorageDirectory() + "//" + System.currentTimeMillis() + ".jpg";
        File file = new File(screenShotFielPath);
        try {
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileOutputStream out = new FileOutputStream(new File(screenShotFielPath));
            if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
                out.flush();
                out.close();
            }
            Toast.makeText(MapActivity.this, "截屏成功", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            android.util.Log.e(TAG, e.getMessage());
        } finally {
            清理绘图缓存,释放资源
            wView.destroyDrawingCache();
            intent.setDataAndType(Uri.fromFile(new File(screenShotFielPath)), "image/*");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
//            startActivityForResult(intent, FILE_CAMERA_RESULT_CODE);
            onActivityResultAboveL(intent);
        }
    }

你可能感兴趣的:(android)