我使用此类代码从URL下载图片:
public static Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
当我发送URL“http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png”它工作正常,但当我使用“http://www.hospimedica .com / images / stories / articles / article_images / _CC / 20110328%20-%20DJB146.gif“它返回null。 这个网址有什么问题?
答案 0 :(得分:0)
为什么要编写自己的方法来下载图像? Android有内置的方法来实现这一点..只需使用
URL url = new URL("Your url");
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());