【问题标题】:How to convert imageurl to drawable?如何将imageurl转换为drawable?
【发布时间】:2017-08-26 12:33:22
【问题描述】:

首先我搜索了很多,但没有找到适合我的情况的解决方案,我需要将 imageUrl 转换为可绘制的,我尝试过的:

File imageFile = DiskCacheUtils.findInCache(c.profilePhoto, com.nostra13.universalimageloader.core.ImageLoader.getInstance().getDiscCache());

            String file_target = "file://" + imageFile.getPath();


            BitmapDrawable bitDraw = (BitmapDrawable) BitmapDrawable.createFromPath(file_target);
            if (profilePhotos.size() == 4) break;
            BitmapDrawable drawable = bitDraw; //getResources().getDrawable(c.profilePhoto);
            drawable.setBounds(0, 0, width, height);
            profilePhotos.add(drawable);

我也尝试通过 Glide 将图像加载到位图中,但没有成功。

我怎样才能做到这一点?

提前致谢。

【问题讨论】:

  • file_target 不是路径。 imageFile.getPath() 是一条路径。 createFromPath(),根据其名称,似乎需要一个路径,而您没有提供路径。除此之外,请随意用实际的计算机编程术语解释您的代码 sn-p 存在哪些问题,以及对于 Glide 而言“但没有工作”是什么意思。
  • 你试过Drawable#createFromStream()吗?
  • @sam 你的两个网址都没有提供图片路径。
  • @CommonsWare 很好,我尝试通过 ImageLoader 获取缓存的图像,以获取使我将其用作可绘制的路径图像。但它总是因 NPE 异常或许多其他异常而失败,我如何才能得到最终结果? (来自 url 的可绘制图像)。

标签: android android-drawable android-image android-glide


【解决方案1】:

首先将 URL 转换为 Bitmap 并将位图传递给这些函数

Drawable d= new BitmapDrawable(context.getResources(), bitmap); 返回 d;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 2012-07-06
    • 2012-02-11
    • 2013-03-26
    相关资源
    最近更新 更多