-1

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

任何帮助是极大的赞赏。

4

1 回答 1

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 
}
于 2018-08-17T16:58:44.863 回答