Android 从URL下载图像时出现问题
我使用以下代码从URL下载图像:Android 从URL下载图像时出现问题,android,bitmap,Android,Bitmap,我使用以下代码从URL下载图像: public static Bitmap downloadImage(String url) { Bitmap bitmap = null; InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream(),
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“它返回空值。
此URL有什么问题?为什么要编写自己的方法来下载图像?Android内置了实现这一点的方法。。只用
URL url = new URL("Your url");
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
它也不起作用。URL有点问题,但我不知道到底是什么,可能是因为URL中有空格?它不会引发异常。只需在bitmapSee中返回null,请参见此处的解决方案