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);
        }
    }

相关推荐

  1. Android方法

    2024-02-07 02:48:01       36 阅读
  2. Android 10.0 流程

    2024-02-07 02:48:01       47 阅读
  3. Android10以上MediaProjection

    2024-02-07 02:48:01       98 阅读
  4. Android11系统去掉功能

    2024-02-07 02:48:01       28 阅读
  5. iOS 防方法(一)

    2024-02-07 02:48:01       60 阅读
  6. 高通Android 12/Android 13

    2024-02-07 02:48:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 02:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 02:48:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 02:48:01       82 阅读
  4. Python语言-面向对象

    2024-02-07 02:48:01       91 阅读

热门阅读

  1. C++枚举算法(3)

    2024-02-07 02:48:01       59 阅读
  2. QT 应用程序中集成浏览器

    2024-02-07 02:48:01       46 阅读
  3. js 基础

    js 基础

    2024-02-07 02:48:01      36 阅读
  4. 关于自动驾驶概念的学习和一些理解

    2024-02-07 02:48:01       54 阅读
  5. CSS选择器及其优先级

    2024-02-07 02:48:01       51 阅读
  6. docker安装gitlab-runner

    2024-02-07 02:48:01       49 阅读