【问题标题】:Capture a screenshot of activity and saving on sdcard捕获活动的屏幕截图并保存在 sdcard 上
【发布时间】:2014-04-17 04:10:07
【问题描述】:

我正在尝试截取活动的屏幕截图,以便在图库中看到它。但是代码不起作用,并且图像没有显示在图库中。我在按钮单击事件中调用方法 takePicSave()。 同样在清单文件中,权限也在那里。

   private void takePicSave()
    {

        Bitmap bitmap = takeScreenshot();
        saveBitmap(bitmap);

    }

      public Bitmap takeScreenshot(){

        View rootview = findViewById(android.R.id.content).getRootView();
        rootview.setDrawingCacheEnabled(true);
        return rootview.getDrawingCache();

      }

      public void saveBitmap(Bitmap bitmap){

        Date date = new Date() ; 
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());


          File imagePath = new File(Environment.getExternalStorageDirectory() , dateFormat.format(date) + " .png");
          FileOutputStream fos;
            try {
                fos = new FileOutputStream(imagePath);
                bitmap.compress(CompressFormat.JPEG, 100, fos);
                fos.flush();
                fos.close();
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory())));


            } catch (FileNotFoundException e) {
                Log.e("Error " , e.getMessage(), e);
            } catch (IOException e) {
                Log.e("Error ", e.getMessage(), e);
            }


        }



      }

我得到的错误 “java.io.FileNotFoundException/mnt/sdcard/2014-04-16.png(权限被拒绝)

【问题讨论】:

  • 您的清单中有读写权限吗?
  • 目前我只有在 manifest 中有写权限
  • 图像在 android 2.2、4.0 和 4.1 的图库中显示,但在 4.4、2.3 和 2.2 上失败。为什么它对某些人起作用而不对其他人起作用?请帮我理解

标签: android


【解决方案1】:

我已经做到了,在我的一个项目中你可以试试下面的功能

public static Bitmap takeScreenShotOfCurrentSticky(Activity activity,View v)
    {
        View view = v;
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        b = Bitmap.createBitmap(b1);
        view.destroyDrawingCache();
        System.out.println("~~~~Bitmap frm Screen shot"+b);
        return b;
    }

我已将此函数放在 Application 类中,以便传递我们想要截屏的活动对象和视图。您可以根据自己的要求进行更改。

【讨论】:

    【解决方案2】:

    确保您已在清单文件中添加读写外部存储的权限。

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    【讨论】:

    • 我确实添加了现在阅读的权限,但图库中仍然没有显示图像
    • 04-17 03:43:05.292:E/错误(377):/mnt/sdcard/2014-04-17 .png(权限被拒绝)04-17 03:43:05.292:E /Error(377): java.io.FileNotFoundException: /mnt/sdcard/2014-04-17 .png (Permission denied) 04-17 03:43:05.292: E/Error(377): at org.apache.harmony .luni.platform.OSFileSystem.open(Native Method) 04-17 03:43:05.292: E/Error(377): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232) 04-17 03: 43:05.292: E/Error(377): at java.io.FileOutputStream.(FileOutputStream.java:94) 04-17 03:43:05.292: E/Error(377):
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多