我从图库中获取图像并发送到我的远程服务器。接下来,我将图像路径保存到我的共享首选项中。这很好用。 过了一段时间,当我尝试从保存的路径恢复图像并在我的图像视图中显示时,我在行中得到了这个异常:
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), fileUri);
有时,代码可以正常运行,但过了一段时间后我就会遇到这个异常。
我的例外是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.techabout.medappointment, PID: 4160
java.lang.SecurityException: Permission Denial: opening provider com.android.externalstorage.ExternalStorageProvider from ProcessRecord{9922e7c 4160:net.techabout.medappointment/u0a105} (pid=4160, uid=10105) requires android.permission.MANAGE_DOCUMENTS or
android.permission.MANAGE_DOCUMENTS
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3550)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4778)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2018)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1468)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1088)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:942)
at android.content.ContentResolver.openInputStream(ContentResolver.java:662)
at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:866)
at net.techabout.medappointment.ConfigurationFragment.onCreateView(ConfigurationFragment.java:115)
这是我的清单:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
这是我的代码:
private Uri fileUri;
String pathimg;
Bitmap bitmap = null;
ImageView imageView;
private static int RESULT_LOAD_IMG = 1;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.configuationfrag, container, false);
imageView = (ImageView) view.findViewById(R.id.imageView);
SharedPreferences pref = getActivity().getSharedPreferences(Common.SHARED_PREF_NAME, Context.MODE_PRIVATE);
pathimg = pref.getString("imagepath", "");
fileUri = Uri.parse(pathimg);
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), fileUri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Button buttonChoose = (Button) view.findViewById(R.id.buttonChooseImage);
buttonChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showFileChooser();
}
});
Button buttonupload = (Button) view.findViewById(R.id.buttonUpload);
buttonupload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (bitmap != null) new UploadImage().execute();
}
});
return view;
}
private void showFileChooser() {
Intent intent;
if (Build.VERSION.SDK_INT < 19) {
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, RESULT_LOAD_IMG);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, RESULT_LOAD_IMG);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
pathimg=filePath.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:4)
问题是框架只授予对图像URI的临时访问权限。 如果您想继续使用您必须请求持久权限的URI,请在将URI存储到共享首选项之前执行以下操作:
final int takeFlags = intent.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(uri, takeFlags);
有关详细信息,请查看documentation。