我正在尝试从服务器下载图像,但总是抛出异常。有人能告诉我为什么会发生这种情况,以及正确的方法是什么吗?
public static String getBitmap(String url) throws IOException {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap bmp= BitmapFactory.decodeStream(is);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] b=stream.toByteArray();
String encoded = Base64.encodeToString(b, Base64.DEFAULT);
is.close();
return encoded;
}
Picasso
或Glide
库来加载服务器图片。 - Piyush