我正在使用以下代码在我的ImageView中显示位图。 当我尝试加载大小比例大于1.5MB的图像时,它给我错误。 任何人建议我的解决方案?如何处理内存不足错误?
try {
URL aURL = new URL(myRemoteImages[val]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = null;
try
{
is= conn.getInputStream();
}catch(IOException e)
{
return 0;
}
int a= conn.getConnectTimeout();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm;
try
{
bm = BitmapFactory.decodeStream(bis);
}catch(Exception ex)
{
bis.close();
is.close();
return 0;
}
bis.close();
is.close();
img.setImageBitmap(bm);
} catch (IOException e) {
return 0;
}
return 1;
登录猫:
06-14 12:03:11.701: ERROR/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception
06-14 12:03:11.861: ERROR/AndroidRuntime(443): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-14 12:03:11.861: ERROR/AndroidRuntime(443): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
为了避免java.lang.OutOfMemory例外,对其进行解码,除非你绝对信任来源为您提供了可用内存内舒适适合预见的大小的图像数据之前检查的位图的尺寸。 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(),R.id.myimage,options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType; – hitesh141 2015-04-08 05:35:39