Glide未加载从图库中拾取的图像。尽管提供了读写权限,它始终显示以下错误。
java.io.FileNotFoundException: /storage/emulated/0/Pictures/JPEG_20200120103701_3365226945725752825.jpg: open failed: EACCES (Permission denied)
添加此行android清单
android:requestLegacyExternalStorage =“ true”
没有Gallery应用程序会给您该文件路径。
所以您一直在弄乱诸如getRealPathFromUri()之类的东西。
这不会对Q起作用,因为没有人可以访问该图片路径。
最好直接使用获得的uri。
您也应该在问题下方执行此操作。
制作文件实例并传递您要加载到ImageView中的文件的uri如果失败,您也可以使用RequestListener跟踪错误
Glide.with(context)
.load(new File(fileUri.getPath())) // Uri of the picture
.into(imageview)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
Log.e("xmx1","Error "+e.toString());
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Log.e("xmx1","no Error ");
return false;
}
});
确保已添加
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
在您的应用程序标记中`''
<application
android:name=".App"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
... >
您必须提供运行时存储权限。如果需要更多帮助,请仔细阅读此documentation。
[首先,我们必须通过警报对话框来打扰用户,例如读取联系人列表。另外,我们还需要提供解释,说明为什么我们要请求许可,然后您可以按照下面的定义处理其余部分。
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
arrayOf(Manifest.permission.READ_CONTACTS),
MY_PERMISSIONS_REQUEST_READ_CONTACTS)
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}