耗尽内存的Andr​​oid项目 [英] run out of memory android project

查看:139
本文介绍了耗尽内存的Andr​​oid项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动和运行我的真实设备的发言权遗憾的是停在和应用程序的logcat说java.lang.runoutofmemory,我不知道如何解决它。

When i start and run my app on the real device its say unfortunately stopped and in the logcat says java.lang.runoutofmemory and i don't know how to fix it

下面是我的主要活动类:

Here is my main activity class:

public class MainActivity extends Activity implements OnItemClickListener,
TextWatcher {


String[] category_name;
int[] category_id;
Bitmap[] pics;
GridView v1;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ActionBar bar = getActionBar();
// for color
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#5c7afe")));
// for image

setContentView(R.layout.activity_main);

v1 = (GridView) findViewById(R.id.gridvieww);
SetDB();
v1.setOnItemClickListener(this);
}

public void SetDB() {
SQLiteDatabase DB = openOrCreateDatabase("test", MODE_PRIVATE, null);// to
                                                                        // create
                                                                        // db
                                                                        // folder
InputStream is = getResources().openRawResource(R.raw.hotline_data);
try {
    byte[] b = new byte[is.available()];
    is.read(b);
    FileOutputStream fos = new FileOutputStream("/data/data/"
            + getPackageName() + "/databases/Hotline.sqlite");
    fos.write(b);
    fos.close();

    SQLiteDatabase hotline_db = openOrCreateDatabase("Hotline.sqlite",
            MODE_PRIVATE, null);
    Cursor c = hotline_db.rawQuery("select * from category", null);
    if (c.moveToFirst()) {
        category_name = new String[c.getCount()];
        category_id = new int[c.getCount()];
        pics = new Bitmap[c.getCount()];
        int i = 0;
        do {
            int id = c.getInt(c.getColumnIndex("id"));
            String name = c.getString(c.getColumnIndex("name"));
            byte[] b1 = c.getBlob(c.getColumnIndex("pic"));
            Bitmap bm = BitmapFactory.decodeByteArray(b1, 0, b1.length);
            category_name[i] = name;
            category_id[i] = id;
            pics[i] = bm;
            i++;
        } while (c.moveToNext());
        CustomGridAdapter cga = new CustomGridAdapter(this,
                category_name, pics);
        v1.setAdapter(cga);
     }

  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

 }

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(this, Company.class);
i.putExtra("cat_id", category_id[arg2]);
startActivity(i);
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
// TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

}

和现在,这里的网格视图类

and now here's the grid view class

public class CustomGridAdapter extends BaseAdapter {
private Context context;
private final String [] gridValues;
private Bitmap[] pic;
public CustomGridAdapter (Context context,String[] gridValues,Bitmap[] pic){
this.context= context;
this.gridValues=gridValues;
this.pic=pic;


}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return gridValues.length;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub

    LayoutInflater li=(LayoutInflater)                                  
    View v=li.inflate(R.layout.grid, arg2, false);
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ImageView iv=(ImageView) v.findViewById(R.id.gridimage);

    iv.setImageBitmap(pic[arg0]);

    return v;
    }

    }

和这里的日志猫

08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.view.Choreographer.doFrame(Choreographer.java:573)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.os.Handler.handleCallback(Handler.java:733)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.os.Looper.loop(Looper.java:157)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.app.ActivityThread.main(ActivityThread.java:5356)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Method.invoke(Method.java:515)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at dalvik.system.NativeStart.main(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021): Caused by: java.lang.reflect.InvocationTargetException
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Constructor.constructNative(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
08-20 18:09:41.738: E/AndroidRuntime(11021):    ... 41 more
08-20 18:09:41.738: E/AndroidRuntime(11021): Caused by: java.lang.OutOfMemoryError
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.content.res.Resources.loadDrawable(Resources.java:3056)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.widget.ImageView.<init>(ImageView.java:133)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.widget.ImageView.<init>(ImageView.java:123)

需要你的帮助,请迅速

need your help please quickly

推荐答案

您正试图在运行时加载太多的位图。虽然Android设备可能有内存相当数量,操作系统只分配少量用于运行应用程序,所以你可能会遇到OutOfMemmoryError如果你持有足够了。

You're trying to load too many bitmaps during run-time. While Android devices might have a fair amount of memory, the OS only allocates a small amount for running apps, and so you'll likely experience an OutOfMemmoryError if you hold enough.

在我的应用程序,我需要举行的1024×1024的图像缓冲区,并开始后大约3-4被装越来越内存溢出错误。

In my app, I needed to hold a buffer of 1024x1024 images, and started getting OutOfMemory errors after about 3-4 were loaded.

一种选择来解决,这是设置在清单文件一个largeHeap设置的存储器然而这是添加是有限的量,和不同的设备之间变化。最后,你也许可以装载一对夫妇更多的图像,但你还是会达到同样的死胡同。

One option to solve this is to set a largeHeap setting in the manifest file, however the amount of memory which is added is limited, and varies between different devices. At the end, you might be able to load a couple more images, but you'll still reach the same dead end.

真正的方式来解决,这是想出了一个办法,不需要很多的位图一次运行时。找出你需要在屏幕上显示在任何给定时刻,只加载那些图片。正在显示任何位图。它实际上不是,或即将很快,应该被删除。当删除位图时,一定要要求他们回收()函数,因为这瞬间释放了位图的内存。

The real way to solve this is to come up with a way to not require many bitmaps at once during runtime. Figure out which images you need to display on screen at any given moment and only load those. Any bitmap which isn't actually being displayed, or about to be very shortly, should be removed. When removing bitmaps, be sure to call the recycle() function on them, as this frees up the bitmap's memory instantly.

这应该让你开始。

这篇关于耗尽内存的Andr​​oid项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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