我正在使用以下代码从url获取位图。在我的代码和第三次或第四次调用崩溃中多次使用此函数,显示以下错误。网址存在,我确信之前的通话没有区别。谢谢
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input); //crashes at this line
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
06-15 14:15:34.365: E/AndroidRuntime(863): FATAL EXCEPTION: main
06-15 14:15:34.365: E/AndroidRuntime(863): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-15 14:15:34.365: E/AndroidRuntime(863): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-15 14:15:34.365: E/AndroidRuntime(863): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
06-15 14:15:34.365: E/AndroidRuntime(863): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:515)
06-15 14:15:34.365: E/AndroidRuntime(863): at com.nimbosolutions.projecte1.TabBar_iOSActivity.getBitmapFromURL(TabBar_iOSActivity.java:232)
06-15 14:15:34.365: E/AndroidRuntime(863): at com.nimbosolutions.projecte1.TabBar_iOSActivity.addTab(TabBar_iOSActivity.java:209)
答案 0 :(得分:0)
你说“第三次或第四次” - 每次都是相同的位图吗?如果是,为什么不缓存和重用?
如果不是,你真的需要同时在内存中的所有3-4位图吗?如果是的话,你会受到严厉批评。如果没有,我可以建议你在丢弃它们之前在你的位图上调用recycle()
(即在你引用之前)吗?众所周知,释放内存的速度比vanilla GC快。