gpt4 book ai didi

android - SecurityException 从 Android 库中读取图片

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:45 25 4
gpt4 key购买 nike

我正在从图库中获取图像并发送到我的远程服务器。接下来,我将图像路径保存到我的共享首选项中。这很好用。一段时间后,当我尝试从保存的路径中恢复图像并在我的 ImageView 中显示时,我在行中得到了这个异常:

bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), fileUri);

有时,代码可以工作,但过了一段时间后我得到了这个异常。

我的异常(exception)是:

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)

这是我的 list :

<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();
}
}
}

最佳答案

问题在于框架只授予对图像 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 .

关于android - SecurityException 从 Android 库中读取图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40402609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com