我正在尝试从远程服务器下载图像,下载的图像数量为30.我用来下载图像的代码如下。某些图像下载成功,某些图像无法下载并引发上述异常。可能是什么问题。
public static Bitmap loadBitmap(String url)
{
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), 4*1024);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, 4 * 1024);
int byte_;
while ((byte_ = in.read()) != -1)
out.write(byte_);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e("","Could not load Bitmap from: " + url);
} finally {
try{
in.close();
out.close();
}catch( IOException e )
{
System.out.println(e);
}
}
return bitmap;
}
答案 0 :(得分:1)
答案 1 :(得分:0)
在我的情况下,我通过编码网址解决了这个错误 因为我想下载的图片网址中包含波斯字母(或其他Unicode字符) 所以我用编码的UTF-8字母替换了所有波斯语字符