java.lang.OutOfMemoryError

问题描述 投票:0回答:1

我收到了一个错误的错误。我从sqlite db获取人员信息。用光标取整个表然后放入arraylist。信息包括姓名,公司,职位,地址,电话,手机,电子邮件和照片(以字节为单位)。这是代码:

public class Contacts extends ListActivity {

private ArrayList<Stuff> contactsList = null;
private ContactsAdapter contactsAdapter;
private ProgressDialog m_ProgressDialog = null;
private Runnable viewContacts;

// Attributes

private static String name = "";
private static String company = "";
private static String title = "";
private static String address = "";
private static String phone = "";
private static String cel = "";
private static String email = "";
private static byte[] picture = null;
private static int template = 0;

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_list);

    contactsList = new ArrayList<Stuff>();
    contactsAdapter = new ContactsAdapter(this,
            R.layout.contact_list_entry, contactsList);

    setListAdapter(this.contactsAdapter);

    // Setting progress dialog
    viewContacts = new Runnable() {
        public void run() {
            getOrders();
        }
    };
    Thread thread = new Thread(null, viewContacts, "MagentoBackground");
    thread.start();
    m_ProgressDialog = ProgressDialog.show(Contacts.this, "Please wait...",
            "Retrieving data ...", true);

    /*
     * 
     * Log.d("*********", "size :" + Integer.toString(contactsList.size()));
     * 
     * if (contactsList != null && !contactsList.isEmpty()) {
     * 
     * contactsAdapter.notifyDataSetChanged(); contactsAdapter.clear(); for
     * (int i = 0; i < contactsList.size(); i++) {
     * contactsAdapter.add(contactsList.get(i)); } }
     * 
     * contactsAdapter.notifyDataSetChanged();
     */

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    super.onListItemClick(l, v, position, id);

    Stuff stuff = contactsList.get(position);

    Intent i = new Intent(Contacts.this, ContactsDetail.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable("person", stuff);
    i.putExtra("personBundle", bundle);
    startActivity(i);

}

private Runnable returnRes = new Runnable() {

    public void run() {
        // Get Data from database

        CardsDatabase info = new CardsDatabase(Contacts.this);
        info.open();
        Cursor c = info.geTContactsData();
        c.moveToFirst();

        while (c.isAfterLast() == false) {
            name = c.getString(c.getColumnIndex(CardsDatabase.NAME_R));
            company = c
                    .getString(c.getColumnIndex(CardsDatabase.COMPANY_R));
            title = c.getString(c.getColumnIndex(CardsDatabase.TITLE_R));
            address = c
                    .getString(c.getColumnIndex(CardsDatabase.ADDRESS_R));
            phone = c.getString(c.getColumnIndex(CardsDatabase.PHONE_R));
            cel = c.getString(c.getColumnIndex(CardsDatabase.CEL_R));
            email = c.getString(c.getColumnIndex(CardsDatabase.EMAIL_R));
            picture = c.getBlob(c.getColumnIndex(CardsDatabase.PHOTO_R));
            template = Integer.parseInt(c.getString(c
                    .getColumnIndex(CardsDatabase.TEMPLATE_R)));
            Stuff s = new Stuff(name, company, title, address, phone, cel,
                    email, template, null, true);
            contactsList.add(s);

            c.moveToNext();
        }

        c.close();
        info.close();

        if (contactsList != null && contactsList.size() > 0) {
            contactsAdapter.notifyDataSetChanged();
            for (int i = 0; i < contactsList.size(); i++)
                contactsAdapter.add(contactsList.get(i));
        }
        m_ProgressDialog.dismiss();
        contactsAdapter.notifyDataSetChanged();
    }
};

private void getOrders() {
    runOnUiThread(returnRes);
}

}

我使用的这个自定义适配器:

public class ContactsAdapter extends ArrayAdapter<Stuff> {

private ArrayList<Stuff> contactDataItems;

private Activity context;

public ContactsAdapter(Activity context, int textViewResourceId,
        ArrayList<Stuff> contactDataItems) {
    super(context, textViewResourceId, contactDataItems);
    this.context = context;
    this.contactDataItems = contactDataItems;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if (view == null) {
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.contact_list_entry, null);
    }

    Stuff stuff = contactDataItems.get(position);

    if (stuff != null) {

        // name
        TextView nameTextView = (TextView) view
                .findViewById(R.id.contact_name);
        nameTextView.setText(stuff.getName());

        // number
        TextView ratingTextView = (TextView) view
                .findViewById(R.id.contact_number);
        ratingTextView.setText("Rating: " + stuff.getPhone());

        // thumb image
        ImageView imageView = (ImageView) view
                .findViewById(R.id.contact_pic);
        final Bitmap bitmap;
        byte[] picture = stuff.getPicture();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        if (picture != null) {
            ByteArrayInputStream imageStream = new ByteArrayInputStream(
                    picture);
            bitmap = BitmapFactory.decodeStream(imageStream);
            Log.d("populate", "stored pic");
        } else {

            bitmap = BitmapFactory.decodeResource(Resources.getSystem(),
                    R.drawable.default_pic);
            bitmap.compress(CompressFormat.PNG, 0, bos);
            picture = bos.toByteArray();
            Log.d("populate", "default pic");

        }

        imageView.setImageBitmap(bitmap);

    }

    return view;

}
}

Logcat显示以下消息:

02-27 21:43:42.178: D/dalvikvm(21573): GC_FOR_MALLOC freed 5713 objects / 551400 bytes in 46ms
02-27 21:43:42.389: D/dalvikvm(21573): GC_FOR_MALLOC freed 11769 objects / 475280 bytes in 36ms
02-27 21:43:42.436: D/dalvikvm(21573): GC_FOR_MALLOC freed 819 objects / 26704 bytes in 36ms
02-27 21:43:42.600: D/dalvikvm(21573): GC_FOR_MALLOC freed 8720 objects / 349320 bytes in 38ms
02-27 21:43:42.600: I/dalvikvm-heap(21573): Grow heap (frag case) to 3.212MB for 156920-byte allocation
02-27 21:43:42.647: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 104624 bytes in 46ms
02-27 21:43:42.881: D/dalvikvm(21573): GC_FOR_MALLOC freed 13077 objects / 419024 bytes in 37ms
02-27 21:43:42.881: I/dalvikvm-heap(21573): Grow heap (frag case) to 3.337MB for 235372-byte allocation
02-27 21:43:42.936: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 156928 bytes in 52ms
02-27 21:43:43.210: D/dalvikvm(21573): GC_FOR_MALLOC freed 16366 objects / 524264 bytes in 38ms
02-27 21:43:43.288: D/dalvikvm(21573): GC_FOR_MALLOC freed 3251 objects / 104584 bytes in 35ms
02-27 21:43:43.288: I/dalvikvm-heap(21573): Grow heap (frag case) to 3.524MB for 353048-byte allocation
02-27 21:43:43.342: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 235376 bytes in 49ms
02-27 21:43:43.624: D/dalvikvm(21573): GC_FOR_MALLOC freed 16524 objects / 531352 bytes in 42ms
02-27 21:43:44.092: D/dalvikvm(21573): GC_FOR_MALLOC freed 13058 objects / 418136 bytes in 104ms
02-27 21:43:44.092: I/dalvikvm-heap(21573): Grow heap (frag case) to 3.798MB for 529564-byte allocation
02-27 21:43:44.147: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 353056 bytes in 56ms
02-27 21:43:44.452: D/dalvikvm(21573): GC_FOR_MALLOC freed 16365 objects / 524240 bytes in 44ms
02-27 21:43:44.733: D/dalvikvm(21573): GC_FOR_MALLOC freed 16366 objects / 524272 bytes in 45ms
02-27 21:43:44.944: D/dalvikvm(21573): GC_FOR_MALLOC freed 11404 objects / 365480 bytes in 43ms
02-27 21:43:44.944: I/dalvikvm-heap(21573): Grow heap (frag case) to 4.219MB for 794336-byte allocation
02-27 21:43:44.999: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 529568 bytes in 53ms
02-27 21:43:45.280: D/dalvikvm(21573): GC_FOR_MALLOC freed 16365 objects / 524240 bytes in 47ms
02-27 21:43:45.577: D/dalvikvm(21573): GC_FOR_MALLOC freed 16367 objects / 524296 bytes in 47ms
02-27 21:43:45.874: D/dalvikvm(21573): GC_FOR_MALLOC freed 16366 objects / 524272 bytes in 44ms
02-27 21:43:46.155: D/dalvikvm(21573): GC_FOR_MALLOC freed 16367 objects / 524296 bytes in 47ms
02-27 21:43:46.210: D/dalvikvm(21573): GC_FOR_MALLOC freed 738 objects / 24040 bytes in 44ms
02-27 21:43:46.210: I/dalvikvm-heap(21573): Grow heap (frag case) to 4.850MB for 1191496-byte allocation
02-27 21:43:46.272: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 794344 bytes in 56ms
02-27 21:43:46.592: D/dalvikvm(21573): GC_FOR_MALLOC freed 16365 objects / 524240 bytes in 81ms
02-27 21:43:46.913: D/dalvikvm(21573): GC_FOR_MALLOC freed 16376 objects / 524288 bytes in 51ms
02-27 21:43:47.217: D/dalvikvm(21573): GC_FOR_MALLOC freed 16376 objects / 524288 bytes in 51ms
02-27 21:43:47.546: D/dalvikvm(21573): GC_FOR_MALLOC freed 16366 objects / 524280 bytes in 53ms
02-27 21:43:47.850: D/dalvikvm(21573): GC_FOR_MALLOC freed 16376 objects / 524288 bytes in 51ms
02-27 21:43:48.131: D/dalvikvm(21573): GC_FOR_MALLOC freed 16376 objects / 524288 bytes in 48ms
02-27 21:43:48.194: D/dalvikvm(21573): GC_FOR_MALLOC freed 1069 objects / 34464 bytes in 46ms
02-27 21:43:48.202: I/dalvikvm-heap(21573): Grow heap (frag case) to 5.797MB for 1787236-byte allocation
02-27 21:43:48.288: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 1191504 bytes in 91ms
02-27 21:43:48.647: D/dalvikvm(21573): GC_FOR_MALLOC freed 20800 objects / 665856 bytes in 56ms
02-27 21:43:49.006: D/dalvikvm(21573): GC_FOR_MALLOC freed 20832 objects / 666880 bytes in 60ms
02-27 21:43:49.366: D/dalvikvm(21573): GC_FOR_MALLOC freed 20832 objects / 666880 bytes in 57ms
02-27 21:43:49.733: D/dalvikvm(21573): GC_FOR_MALLOC freed 20822 objects / 666856 bytes in 64ms
02-27 21:43:50.077: D/dalvikvm(21573): GC_FOR_MALLOC freed 20822 objects / 666864 bytes in 56ms
02-27 21:43:50.413: D/dalvikvm(21573): GC_FOR_MALLOC freed 20832 objects / 666880 bytes in 56ms
02-27 21:43:50.756: D/dalvikvm(21573): GC_FOR_MALLOC freed 20832 objects / 666880 bytes in 56ms
02-27 21:43:50.850: D/dalvikvm(21573): GC_FOR_MALLOC freed 3179 objects / 101984 bytes in 54ms
02-27 21:43:50.905: I/dalvikvm-heap(21573): Grow heap (frag case) to 7.217MB for 2680844-byte allocation
02-27 21:43:50.983: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 1787240 bytes in 82ms
02-27 21:43:51.467: D/dalvikvm(21573): GC_FOR_MALLOC freed 30122 objects / 964160 bytes in 73ms
02-27 21:43:52.296: D/dalvikvm(21573): GC_FOR_MALLOC freed 30122 objects / 964160 bytes in 64ms
02-27 21:43:52.764: D/dalvikvm(21573): GC_FOR_MALLOC freed 30124 objects / 964224 bytes in 65ms
02-27 21:43:53.264: D/dalvikvm(21573): GC_FOR_MALLOC freed 30124 objects / 964224 bytes in 66ms
02-27 21:43:53.764: D/dalvikvm(21573): GC_FOR_MALLOC freed 30115 objects / 964240 bytes in 67ms
02-27 21:43:54.241: D/dalvikvm(21573): GC_FOR_MALLOC freed 30124 objects / 964224 bytes in 67ms
02-27 21:43:54.717: D/dalvikvm(21573): GC_FOR_MALLOC freed 30124 objects / 964224 bytes in 68ms
02-27 21:43:54.952: D/dalvikvm(21573): GC_FOR_MALLOC freed 12565 objects / 402336 bytes in 65ms
02-27 21:43:54.952: I/dalvikvm-heap(21573): Grow heap (frag case) to 9.348MB for 4021256-byte allocation
02-27 21:43:55.030: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 2680848 bytes in 79ms
02-27 21:43:55.710: D/dalvikvm(21573): GC_FOR_MALLOC freed 44067 objects / 1410704 bytes in 79ms
02-27 21:43:56.381: D/dalvikvm(21573): GC_FOR_MALLOC freed 44067 objects / 1410704 bytes in 79ms
02-27 21:43:57.100: D/dalvikvm(21573): GC_FOR_MALLOC freed 44067 objects / 1410704 bytes in 81ms
02-27 21:43:57.788: D/dalvikvm(21573): GC_FOR_MALLOC freed 44067 objects / 1410704 bytes in 82ms
02-27 21:43:58.491: D/dalvikvm(21573): GC_FOR_MALLOC freed 44067 objects / 1410704 bytes in 83ms
02-27 21:43:59.186: D/dalvikvm(21573): GC_FOR_MALLOC freed 44067 objects / 1410704 bytes in 84ms
02-27 21:43:59.874: D/dalvikvm(21573): GC_FOR_MALLOC freed 44067 objects / 1410704 bytes in 85ms
02-27 21:44:00.405: D/dalvikvm(21573): GC_FOR_MALLOC freed 26650 objects / 853360 bytes in 81ms
02-27 21:44:00.413: I/dalvikvm-heap(21573): Grow heap (frag case) to 12.543MB for 6031876-byte allocation
02-27 21:44:00.506: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 4021264 bytes in 96ms
02-27 21:44:01.491: D/dalvikvm(21573): GC_FOR_MALLOC freed 65011 objects / 2080912 bytes in 105ms
02-27 21:44:02.694: D/dalvikvm(21573): GC_FOR_MALLOC freed 65021 objects / 2080928 bytes in 107ms
02-27 21:44:03.741: D/dalvikvm(21573): GC_FOR_MALLOC freed 65011 objects / 2080912 bytes in 110ms
02-27 21:44:04.780: D/dalvikvm(21573): GC_FOR_MALLOC freed 65021 objects / 2080928 bytes in 110ms
02-27 21:44:05.827: D/dalvikvm(21573): GC_FOR_MALLOC freed 65011 objects / 2080912 bytes in 114ms
02-27 21:44:06.858: D/dalvikvm(21573): GC_FOR_MALLOC freed 65021 objects / 2080928 bytes in 111ms
02-27 21:44:07.913: D/dalvikvm(21573): GC_FOR_MALLOC freed 65011 objects / 2080912 bytes in 117ms
02-27 21:44:08.702: D/dalvikvm(21573): GC_FOR_MALLOC freed 47564 objects / 1522304 bytes in 114ms
02-27 21:44:08.991: I/dalvikvm-heap(21573): Grow heap (frag case) to 17.337MB for 9047804-byte allocation
02-27 21:44:09.225: D/dalvikvm(21573): GC_FOR_MALLOC freed 1 objects / 6031880 bytes in 235ms
02-27 21:44:10.303: D/dalvikvm(21573): GC_FOR_MALLOC freed 65527 objects / 2097120 bytes in 137ms
02-27 21:44:11.374: D/dalvikvm(21573): GC_FOR_MALLOC freed 65528 objects / 2097152 bytes in 143ms
02-27 21:44:12.530: D/dalvikvm(21573): GC_FOR_MALLOC freed 65528 objects / 2097152 bytes in 132ms
02-27 21:44:13.553: D/dalvikvm(21573): GC_FOR_MALLOC freed 65528 objects / 2097152 bytes in 133ms
02-27 21:44:14.577: D/dalvikvm(21573): GC_FOR_MALLOC freed 65518 objects / 2097136 bytes in 135ms
02-27 21:44:15.608: D/dalvikvm(21573): GC_FOR_MALLOC freed 65528 objects / 2097152 bytes in 139ms
02-27 21:44:16.655: D/dalvikvm(21573): GC_FOR_MALLOC freed 65528 objects / 2097152 bytes in 139ms
02-27 21:44:17.983: D/dalvikvm(21573): GC_FOR_MALLOC freed 65522 objects / 2096960 bytes in 141ms
02-27 21:44:19.014: D/dalvikvm(21573): GC_FOR_MALLOC freed 65528 objects / 2097152 bytes in 147ms
02-27 21:44:20.038: D/dalvikvm(21573): GC_FOR_MALLOC freed 65518 objects / 2097136 bytes in 144ms
02-27 21:44:21.069: D/dalvikvm(21573): GC_FOR_MALLOC freed 65528 objects / 2097152 bytes in 150ms
02-27 21:44:21.655: D/dalvikvm(21573): GC_FOR_MALLOC freed 33225 objects / 1063456 bytes in 139ms
02-27 21:44:21.655: I/dalvikvm-heap(21573): Forcing collection of SoftReferences for 13571696-byte allocation
02-27 21:44:21.788: D/dalvikvm(21573): GC_FOR_MALLOC freed 0 objects / 0 bytes in 133ms
02-27 21:44:21.788: E/dalvikvm-heap(21573): Out of memory on a 13571696-byte allocation.
02-27 21:44:21.788: I/dalvikvm(21573): "main" prio=5 tid=1 RUNNABLE
02-27 21:44:21.788: I/dalvikvm(21573):   | group="main" sCount=0 dsCount=0 obj=0x40020990 self=0xce28
02-27 21:44:21.788: I/dalvikvm(21573):   | sysTid=21573 nice=0 sched=0/0 cgrp=default handle=-1345017736
02-27 21:44:21.788: I/dalvikvm(21573):   | schedstat=( 36479370361 3853179681 6072 )
02-27 21:44:21.788: I/dalvikvm(21573):   at java.util.ArrayList.add(ArrayList.java:~123)
02-27 21:44:21.788: I/dalvikvm(21573):   at android.widget.ArrayAdapter.add(ArrayAdapter.java:178)
02-27 21:44:21.788: I/dalvikvm(21573):   at com.colony.businesscard.Contacts$1.run(Contacts.java:129)
02-27 21:44:21.788: I/dalvikvm(21573):   at android.os.Handler.handleCallback(Handler.java:587)
02-27 21:44:21.788: I/dalvikvm(21573):   at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 21:44:21.788: I/dalvikvm(21573):   at android.os.Looper.loop(Looper.java:143)
02-27 21:44:21.788: I/dalvikvm(21573):   at android.app.ActivityThread.main(ActivityThread.java:4732)
02-27 21:44:21.788: I/dalvikvm(21573):   at java.lang.reflect.Method.invokeNative(Native Method)
02-27 21:44:21.788: I/dalvikvm(21573):   at java.lang.reflect.Method.invoke(Method.java:521)
02-27 21:44:21.788: I/dalvikvm(21573):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-27 21:44:21.788: I/dalvikvm(21573):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-27 21:44:21.788: I/dalvikvm(21573):   at dalvik.system.NativeStart.main(Native Method)
02-27 21:44:21.796: D/AndroidRuntime(21573): Shutting down VM
02-27 21:44:21.796: W/dalvikvm(21573): threadid=1: thread exiting with uncaught exception (group=0x400208b0)
02-27 21:44:21.803: E/AndroidRuntime(21573): FATAL EXCEPTION: main
02-27 21:44:21.803: E/AndroidRuntime(21573): java.lang.OutOfMemoryError
02-27 21:44:21.803: E/AndroidRuntime(21573):    at java.util.ArrayList.add(ArrayList.java:123)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at android.widget.ArrayAdapter.add(ArrayAdapter.java:178)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at com.colony.businesscard.Contacts$1.run(Contacts.java:129)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at android.os.Handler.handleCallback(Handler.java:587)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at android.os.Looper.loop(Looper.java:143)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at android.app.ActivityThread.main(ActivityThread.java:4732)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at java.lang.reflect.Method.invokeNative(Native Method)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at java.lang.reflect.Method.invoke(Method.java:521)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-27 21:44:21.803: E/AndroidRuntime(21573):    at dalvik.system.NativeStart.main(Native Method)
02-27 21:44:21.881: I/Process(21573): Sending signal. PID: 21573 SIG: 9

我可以告诉我问题出在哪里吗?

android sqlite arraylist out-of-memory
1个回答
0
投票

我建议只将图像路径保存到SQLite DB而不是字节数组。您可以将图像保存到本地目录,然后可以将路径存储到DB中。我希望能帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.