我知道关于同一问题有很多问题,我已经尝试过许多类似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
最佳答案
在gradle文件中添加Picasso库
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) {
}
});
关于android - 从网址转换为位图时,位图返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46219865/