【问题标题】:bitmap return null when converted from url to bitmap位图从 url 转换为位图时返回 null
【发布时间】:2018-02-23 11:35:19
【问题描述】:

我知道有很多关于同一问题的问题,并且我尝试了许多解决方案,例如this,但我仍然得到空字符串。 这是我的代码

try {
      URL url = new URL(myUrl);
      InputStream in = url.openConnection().getInputStream();
      BufferedInputStream bis = new BufferedInputStream(in,1024*8);
      ByteArrayOutputStream out = new ByteArrayOutputStream();

      int len=0;
      byte[] buffer = new byte[1024];
      while((len = bis.read(buffer)) != -1) {
            out.write(buffer, 0, len);
      }
      out.close();
      bis.close();

      byte[] data = out.toByteArray();
      Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);

      d = new BitmapDrawable(getResources(), bmp);

} catch (FileNotFoundException e) {
      d = getResources().getDrawable(R.mipmap.background);
} catch (MalformedURLException e) {
      e.printStackTrace();
} catch (IOException e) {
      e.printStackTrace();
}

myUrl 是 firebase 存储 url

【问题讨论】:

  • 有什么理由不使用像毕加索这样的东西(square.github.io/picasso)?
  • piccaso 和 glide 用于在 imageView 中设置图像,但我只想从 url 绘制
  • 您可以从毕加索获取位图并转换为drawable
  • 怎么样? @BurhanuddinRashid 你能用一段代码解释一下吗

标签: android bitmap drawable firebase-storage


【解决方案1】:

在你的 gradle 文件中添加 Picassso 库

compile 'com.squareup.picasso:picasso:2.5.2'

比像这样使用毕加索下载图像:

 Picasso.with(this)
.load(myUrl)
.into(new Target() {
      @Override
      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 

              //Convert your drawable here
              d = new BitmapDrawable(getResources(), bitmap);

      }

      @Override
      public void onBitmapFailed(Drawable errorDrawable) {

      }

      @Override
      public void onPrepareLoad(Drawable placeHolderDrawable) {

      }
 });

【讨论】:

  • 在位图中仍然得到 "" 空白字符串
  • 您应该如何在位图对象中获取空白字符串...您是否保留了 INTERNET 权限
  • 是的,我已经获得了互联网许可,我从互联网上获取了 url,我检查了它没有返回 null,但是当我从该 url 获取可绘制时,它给了我空白字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多