通过 Uri 滑动加载本地图像。

Glide load local image by Uri.

我正在尝试使用 Glide 从本地存储加载图像但没有成功。

Glide.with(mContext)
    .load(pictureUri) // Uri of the picture
    .transform(new CircleTransform(..))
    .into(profileAvatar);

这是我得到的错误:

java.lang.Exception: Failed to load model: '/storage/emulated/0/DCIM/Camera/IMG_20150831_180900.jpg'
at com.bumptech.glide.request.GenericRequest.onSizeReady(GenericRequest.java:441)
at com.bumptech.glide.request.target.ViewTarget$SizeDeterminer.getSize(ViewTarget.java:211)
at com.bumptech.glide.request.target.ViewTarget.getSize(ViewTarget.java:100)
at com.bumptech.glide.request.GenericRequest.begin(GenericRequest.java:272)
at com.bumptech.glide.manager.RequestTracker.resumeRequests(RequestTracker.java:83)
at com.bumptech.glide.RequestManager.resumeRequests(RequestManager.java:180)
at com.bumptech.glide.RequestManager.onStart(RequestManager.java:203)
at com.bumptech.glide.manager.ActivityFragmentLifecycle.onStart(ActivityFragmentLifecycle.java:50)
at com.bumptech.glide.manager.SupportRequestManagerFragment.onStart(SupportRequestManagerFragment.java:135)
at android.support.v4.app.Fragment.performStart(Fragment.java:1986)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1179)
at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:1996)
at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:176)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:517)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6006)
at android.app.Activity.performRestart(Activity.java:6063)
at android.app.Activity.performResume(Activity.java:6068)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

在等待某人回应时,我尝试从 Uri 创建一个 File 实例并加载它。有用!奇怪!

Glide.with(mContext)
    .load(new File(pictureUri.getPath())) // Uri of the picture
    .transform(new CircleTransform(..))
    .into(profileAvatar);

如果您传递一个 Uri 实例,它最好是合规的,看起来您的实例没有架构 (file://)。

您可以检查 Uri, String and File 模型是如何加载的以揭穿怪异:)

确保 URI 来自文件。

Uri photoUri = Uri.fromFile( new File( preferences.getPhotoUrl()));

Glide.with(this).load( photoUri).into( civUserProfilePhoto);

如果是文件路径,直接传即可。使用 Glide 4.8.0

测试
 Glide.with(context)
  .load(pictureUri.getPath())
  .transform(new CircleTransform(..))
  .into(profileAvatar);

我在没有创建 File 对象的情况下尝试了这个,它工作正常。

public static void setImageUrlFromUri(ImageView imageView, String imageUrl) {
        if (!TextUtils.isEmpty(imageUrl)) {
            Glide.with(imageView.getContext())
                    .load(imageUrl)
                    .into(imageView);
        }
    }

在我的例子中,我 运行 模拟器在 Android 10 上,并且由于安全原因,旧的外部存储访问默认为 disabled, 所以我在 Androidmanifest 文件

中添加了以下代码
<application android:requestLegacyExternalStorage="true" 
.....
.....

</application>

确保您已包含读取和写入外部存储的用户权限