我想使用 ACTION_SEND 分享应用程序的当前 View 。
将当前 View 转换为位图并存储在外部存储中用于将位图转换为可解析的Uri的方法需要权限。
fragment :
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
// View to BitMap
Bitmap b = getScreenShot(getWindow().getDecorView().findViewById(android.R.id.content));
//BitMap to Parsable Uri (needs write permissions)
String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), b,"title", null);
Uri bmpUri = Uri.parse(pathofBmp);
//Share the image
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
这需要WRITE_EXTERNAL_STORAGE 权限, 有没有一种方法可以在不将其存储在 Android 上的外部存储(未经许可)的情况下执行相同的操作?
最佳答案
如果您的minSdkVersion
为19或更高,您可以使用getExternalFilesDir()
将图片写入外部存储,避免权限问题。
您可以将文件写入内部存储(例如,getCacheDir()
),然后使用FileProvider
共享它。无论如何,您都需要使用 FileProvider
或某些 ContentProvider
,如 Android 7.0+ does not like the file
scheme .
如果您想避免磁盘 I/O...您必须将 Bitmap
压缩为 ByteArrayOutputStream
,然后编写您自己的 ContentProvider
从那个 byte[]
服务。这有点冒险,因为如果您的进程在其他应用结束尝试使用 Uri
之前终止,那么您就不走运了,因为您的位图已经消失。
关于java - 通过 ACTION_SEND 共享当前 View 而不将图像存储在 android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493819/