后没有提供持久的文件路径,我要求用户挑选这样一个形象:我保存图像的Uri这样Android 4.4以上版本重启
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
getPreferenceScreen().getSharedPreferences().edit().putString("pic_url", data.getDataString()).apply();
Intent intent = new Intent();
intent.setType("image/*");
if (Build.VERSION.SDK_INT >= 19) {
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
} else
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_image)), PICK_IMAGE_REQUEST);
和获取图像的位图以后这样
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(picUrl));
的问题是,在API 19+从我SharedPreferences不再R中的URI影响用户在重启后选择的图像(我认为在最后一行有一个例外)。
感谢您的解释!但是我不在我的代码中使用'ContentResolver'。你能指导我吗? –
@AlexNewman:“但我不在我的代码中使用ContentResolver” - 根据您的问题,您确实如此。查看你的第三个代码片段。 “你能引导我吗?” - 我不知道你遇到什么问题。 [此示例应用程序](https://github.com/commonsguy/cw-omnibus/tree/v8.3/Documents/Durable)说明了使用这些操作中的任何一个选择图像并采取措施获得对该内容的持久访问权限。它在[我的书](http://commonsware.com/Android/)中有详细介绍。 – CommonsWare