【问题标题】:Save Screenshot into Internal Storage and Sync into Gallery?将屏幕截图保存到内部存储并同步到图库?
【发布时间】:2018-08-17 15:55:45
【问题描述】:

我已经到处搜索了一种方法来做到这一点,我能找到的最好的方法是将屏幕截图保存到 SD 卡中。我想做的是onclick(),截取当前活动的屏幕截图并将其保存在内部存储中,以便用户可以在他们想要的时候在他们的画廊中查看它。

非常感谢任何帮助。

【问题讨论】:

    标签: android android-internal-storage


    【解决方案1】:

    在这里发布完整的代码比较困难。我认为您应该遵循一些教程。

    根据您的要求,我得到的是,您需要使用您的应用程序截取屏幕截图,并将其存储在设备 SD 卡中。

    为此,您应该首先向清单添加适当的权限,

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

    并将以下代码添加到活动中:

    private void takeScreenshot() {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
    
        try {
            // image naming and path  to include sd card  appending name you choose for file, you can change it to your path
            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
    
            // create bitmap screen capture
            View v1 = getWindow().getDecorView().getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
            v1.setDrawingCacheEnabled(false);
    
            File imageFile = new File(mPath);
    
            FileOutputStream outputStream = new FileOutputStream(imageFile);
            int quality = 100;
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
            outputStream.flush();
            outputStream.close();
    
            openScreenshot(imageFile);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    

    此代码将打开生成的图像(截图):

    private void openScreenshot(File imageFile) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(imageFile);
        intent.setDataAndType(uri, "image/*");
        startActivity(intent);
    }
    

    这就是我为我的项目所做的。有时这可能无法满足您的要求。如果不满意,请按照这些教程进行操作,谢谢

    参考列表:http://www.androhub.com/take-a-screenshot-programmatically-in-android/

    http://devdeeds.com/take-screenshot-programmatically/

    https://www.viralandroid.com/2016/01/how-to-take-screenshot-programmatically-in-android.html

    如果您想检查 SD 卡是否可用。这是方法。如果 SD 卡不可用,那么您可以使用内部存储来存储图像。

    Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    Boolean isSDSupportedDevice = Environment.isExternalStorageRemovable();
    
    if(isSDSupportedDevice && isSDPresent)
    {
      // yes SD-card is present
    }
    else
    {
     // SD-card not available 
    }
    

    【讨论】:

    • 这很棒,但我实际上希望将其保存在内部存储中。如果它以前在 SD 卡中,我已经能够做到,但我遇到的问题是不是每个人都有 SD 卡
    • 然后检查SD卡是否存在。如果存在,您可以使用它来存储图像,如果不使用内部存储。现在检查我的答案。谢谢。
    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多