在Android中,如何克服“Out of Memory”错误?

时间:2014-01-23 16:14:02

标签: android sqlite out-of-memory drawable

我有一个使用标签的活动。拉出的选项卡的内容是从SQLite数据库中提取的。每当用户点击标签时,就会对数据库进行呼叫,并显示查看区域中的内容。

内容是ImageViews列表,单击此列表时,会与活动的其他区域进行交互。因为这可以在1 - >之间变化。 n我没有一次加载所有图像并隐藏视图的项目数。这样做可能会导致加载活动时出现一些问题。

标签可以工作但是当我在标签之间切换时,我最终得到内存不足错误:

E/AndroidRuntime(10469): FATAL EXCEPTION: main
E/AndroidRuntime(10469): java.lang.OutOfMemoryError
E/AndroidRuntime(10469):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime(10469):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
E/AndroidRuntime(10469):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
E/AndroidRuntime(10469):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
E/AndroidRuntime(10469):    at android.graphics.drawable.Drawable.createFromStream(Drawable.java:776)
E/AndroidRuntime(10469):    at com.testapplication.testapplicationconfigurator.Configurator$2.createTabContent(Configurator.java:405)
E/AndroidRuntime(10469):    at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:735)
E/AndroidRuntime(10469):    at android.widget.TabHost.setCurrentTab(TabHost.java:430)
E/AndroidRuntime(10469):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:161)
E/AndroidRuntime(10469):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:563)
E/AndroidRuntime(10469):    at android.view.View.performClick(View.java:4383)
E/AndroidRuntime(10469):    at android.view.View$PerformClick.run(View.java:18097)
E/AndroidRuntime(10469):    at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(10469):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(10469):    at android.os.Looper.loop(Looper.java:176)
E/AndroidRuntime(10469):    at android.app.ActivityThread.main(ActivityThread.java:5279)
E/AndroidRuntime(10469):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10469):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(10469):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
E/AndroidRuntime(10469):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
E/AndroidRuntime(10469):    at dalvik.system.NativeStart.main(Native Method)

正在使用的图像是设备上的存储位置,例如

/storage/emulated/0/TestApplicaiton/2/maUdwcbaLU.jpg

我使用该位置将其转换为可绘制对象,并将drawable作为背景放置到ImageView中:

ImageView iv = new ImageView(Configurator.this);
int ivHT = (int) (getResources().getDimension(
        R.dimen.iv_height) / getResources()
        .getDisplayMetrics().density);
LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.MATCH_PARENT);
// set margins (left,top,right,bottom)
params.setMargins(0, 0, 40, 0);
iv.setLayoutParams(params);
iv.getLayoutParams().height = ivHT;
iv.getLayoutParams().width = ivHT;
final String imageLoc = partData.get("productIndexImageLoc");
Log.d("imageLoc",imageLoc);
InputStream imageStream;
try {
    imageStream = new FileInputStream(imageLoc);
    Drawable x = Drawable.createFromStream(imageStream, null);
    iv.setImageDrawable(x);
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

partData只是一个LinkedHashMap,用于存储每个部分的内容和值,例如价格,名称等。

很明显它与Bitmap / Drawable有关,我正在创建留在内存中。当某人在视图中使用新选项卡或从内存中清除该项目时,有没有办法清除使用过的内存?

1 个答案:

答案 0 :(得分:0)

是的,当位图不是更有用时,您可以调用:

bitmap.recycle()

如果您没有这种可能性,可以在清单中添加更多堆(Onli> API 2.3):

android:largeHeap="true"

我建议阅读此文档以找到在android中使用位图的好方法:

Android bitmap memory management