【发布时间】:2020-12-02 13:17:33
【问题描述】:
Android 11 强制要求 scoped storage。在这种情况下,我希望我的应用程序拍摄一张照片并将其保存到某种 应用程序保留的内存中,以便从图库中删除该图片时仍然可用。我正在使用以下方法
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputFileUri = Uri.fromFile(newFile);
if (android.os.Build.VERSION.SDK_INT >= 24) {
outputFileUri = FileProvider.getUriForFile(myActivity, BuildConfig.APPLICATION_ID + ".provider", newFile);
cameraIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.setFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
}
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
问题归结为如何在上面的 sn-p 中创建 newFile,因为 getExternalStoragePublicDirectory() 由于范围存储而不再可用。
我已经阅读了documentation 和各种帖子,但这个问题似乎很广泛和模糊。请避免发布指向MediaStore API 的裸链接。
【问题讨论】:
-
您对“应用程序保留内存”的定义是什么?请注意,该术语目前在the Android SDK documentation 中未使用。
-
CommonsWare 确实如此。我并不是指文档中使用的任何术语。我的意思是,卸载应用程序时删除的任何类型的内存。
-
CommonsWare 换句话说,如果可能的话,我想将我的图片保存在 SharedPreferences 中。
-
使用
FileProvider和getUriForFile()提供internal storage 中的位置,例如getFilesDir()上的Context。在您拥有outputFileUri的ACTION_IMAGE_CAPTURE请求中使用Uri。 -
CommonsWare 谢谢你,解决了我的问题。
标签: android image android-gallery scoped-storage