在Android中,我怎么能战胜我的"内存&QUOT的;错误? [英] In Android, how can I overcome my "Out of Memory" error?

查看:200
本文介绍了在Android中,我怎么能战胜我的"内存&QUOT的;错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用标签的活动。的选项卡的内容被拉动从SQLite数据库拉动。每次显示的呼叫到数据库,并在观看区域的内容做了一个选项卡上的用户水龙头。

I have an activity that uses tabs. The content of the tab is pulled is pulled from the SQLite database. Everytime the user taps on a tab a call is made to the database and the content in the viewing area is displayed.

内容是ImageViews,点击后,与该活动的其他领域互动的名单。我不加载一次和隐藏视图的所有图像项目> N多 - 因为这可以在1有所不同。这样做真的可以导致装载活动有一定的问题。

The content is list of ImageViews that, when clicked, interact with other areas of the activity. Since this can vary between 1 -> n number of items I'm not loading all the images at once and hiding views. Doing this could really cause some issue with loading the activity.

这些标签的工作,但我通过我的标签最终得到之间切换的内存不足的错误:

The tabs work but as I go through switching between tabs I end up getting an out of memory error:

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)

所使用的图像是设备如

The image being used is a stored location on the devices such as

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

我用的位置,使之成为一个可绘制对象,并将绘制为背景给ImageView的:

I use the location to make it into a drawable object and place the drawable as the background to an 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中

partData is just a LinkedHashMap to store content and values for each part such as price, name, etc.

这似乎pretty明显,它是与位图/绘制对象我创建被保留在内存中。有没有一种方法,当有人点击一个新的选项卡,清除使用的内存或从内存中清除项目一旦它的视图中使用?

It seems pretty obvious that it has something to do with the Bitmap/Drawable I'm creating being left in memory. Is there a way to clear that used memory when someone taps a new tab or clear the item from memory once it's used in the view?

推荐答案

是当位图不是更加有用,您可以拨打:

Yes when the bitmap is not more usefull you can call:

bitmap.recycle()

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

If you not have this possibility you can add more heap in manifest(Onli > API 2.3):

android:largeHeap="true"

我建议阅读本文档找到Android的使用位图来工作的好方法:

I suggest to read this documentation to find the good way to work with bitmap in android:

Android的位图内存管理

这篇关于在Android中,我怎么能战胜我的"内存&QUOT的;错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆