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

时间:2015-09-01 12:44:45

标签: android android-glide

我尝试使用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)

7 个答案:

答案 0 :(得分:89)

在等待某人回复时,我尝试从File创建Uri个实例并加载该实例。有用!怪异!

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

答案 1 :(得分:11)

如果你传递'static个实例,那么它最好是合规的,看起来你的没有架构('static)。

您可以查看加载UriStringFile模型的方式,以揭穿古怪的内容:)

答案 2 :(得分:2)

就我而言,我在Android 10上运行了Emulator,并且出于安全原因,默认情况下,旧式外部存储访问权限为disabled, 因此,我在Androidmanifest文件中添加了以下代码

<application android:requestLegacyExternalStorage="true" 
.....
.....

</application>

确保您已包括读写外部存储的用户权限

答案 3 :(得分:1)

确保Uri来自档案。

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

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

答案 4 :(得分:1)

如果它是文件路径,则可以直接传递。经过Glide 4.8.0测试

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

答案 5 :(得分:0)

由于库中的错误,没有工作,仅此工作:

Uri photoUri = Uri.fromFile(new File(preferences.getPhotoUrl()));
Glide.with(this).load(photoUri).into(civUserProfilePhoto);

谢谢:alekshandru

答案 6 :(得分:0)

我不创建File对象就尝试了,效果很好。

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