【问题标题】:Glide does not load image from FileGlide 不从文件加载图像
【发布时间】:2018-06-05 18:04:58
【问题描述】:

我正在使用文件选择器将图像作为文件获取,然后我想使用 Glide 将其加载到 ImageView 中。

我正在尝试使用此代码,但无法将图像加载到图像视图中。

Glide.with(getApplicationContext())
      .load(Uri.fromFile(imageFile))
      .error(R.drawable.ic_person_gray)
      .listener(new RequestListener<Uri, GlideDrawable>() {
        @Override public boolean onException(
            Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource
        ) {
          Timber.d(e.getMessage());
          return false;
        }

        @Override public boolean onResourceReady(
            GlideDrawable resource,
            Uri model,
            Target<GlideDrawable> target,
            boolean isFromMemoryCache,
            boolean isFirstResource
        ) {
          editProfilePresenter.uploadProfilePicture(imageFile);
          return false;
        }
      })
      .into(ivAvatar);

当我进行调试时,它永远不会到达 onExceptiononResourceReady 内的调试点。

我该如何解决这个问题?

【问题讨论】:

  • 而不是像Uri.fromFile(imageFile)那样加载,只需使用load(imageFile.getPath())
  • 我试过了,但结果是一样的。它不会使用 imageFile.getPath 加载图像。
  • 您确定getApplicationContext() 返回一个有效的context 吗?

标签: android android-glide


【解决方案1】:

使用 Uri.parse(imageFile.getAbsolutePath())

【讨论】:

    【解决方案2】:

    那里不需要 Uri,只需获取文件的路径,您的侦听器将如下所示 我的前任:

    Glide.with(context).load(yourFile.getAbsolutePath()).centerCrop().listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            spinKitView.setVisibility(View.GONE);
                            return false;
                        }
    
                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            spinKitView.setVisibility(View.GONE);
                            return false;
                        }
                    }).into(ivAvatar);
    

    SpinkitView 只是一个显示加载动画的视图(以防加载图片需要时间)

    【讨论】:

    • 使用此代码,调试器进入 onResourceReady,但它不显示 ImageView 中加载的图像。你知道为什么吗?
    • 在没有监听器的情况下尝试相同的代码。我猜这是因为 context 传递给它。此外,请确保 ivAvatar 是链接到您的活动/片段的 XML 布局的有效视图
    • 是的,ivAvatar 是一个有效的 ImageView。我删除了一个监听器,但还是一样。
    • 好吧,如果还是不行,并且你确定 isAvatar 是一个有效的视图(也希望你不要在 isAvatar = findViewById(R.id.isAvatar); 之前调用它),这意味着你的文件无效(或者没有图片要加载)
    猜你喜欢
    • 2017-09-04
    • 2019-10-13
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2016-03-30
    相关资源
    最近更新 更多