Android 从url转换为位图时,位图返回null
我知道关于同一个问题有很多问题,我也尝试过很多解决方案,比如,但是我仍然得到了空字符串。 这是我的密码Android 从url转换为位图时,位图返回null,android,bitmap,drawable,firebase-storage,Android,Bitmap,Drawable,Firebase Storage,我知道关于同一个问题有很多问题,我也尝试过很多解决方案,比如,但是我仍然得到了空字符串。 这是我的密码 try { URL url = new URL(myUrl); InputStream in = url.openConnection().getInputStream(); BufferedInputStream bis = new BufferedInputStream(in,1024*8); ByteArrayOutputStream ou
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
将Picasso libarary添加到gradle文件中
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) {
}
});
有什么理由不使用像毕加索()这样的东西吗?piccaso和glide用于在imageView中设置图像,但我只想从URL中绘制。你可以从毕加索那里获取位图并转换为drawablehow@BurhanuddinRashid你能用一段代码解释一下在位图中仍然获得“”空白字符串你应该如何在位图对象中获得空白字符串…你是否保留了INTERNET权限是的,我已经授予了INTERNET权限,我从互联网上获取url,我已经检查了它是否返回空值,但当我从该url获取可绘制的内容时,它会给我一个空白字符串