gpt4 book ai didi

android - 从 url 转换为位图时,位图返回 null

转载 作者:行者123 更新时间:2023-11-29 01:06:57 24 4
gpt4 key购买 nike

我知道关于同一个问题有很多问题,我已经尝试了很多解决方案,比如 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 is firebase storage url

最佳答案

在您的 gradle 文件中添加 Picasso 库

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

比像这样使用 picasso 下载图像:

 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) {

}
});

关于android - 从 url 转换为位图时,位图返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46219865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com