1

Here I am getting failed to allocate memory in bitmap.Here i have attached code with logcat. Can any one give solution for it. I have declared bitmap is null even i am getting same error.

protected Void doInBackground(Void... voids) {
        if (phones != null) {
            Log.e("count", "" + phones.getCount());
            if (phones.getCount() == 0) {
                Log.d("No Contacts", "No Contacts");
            }

            while (phones.moveToNext()) {
               Bitmap bit_thumb = null;
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String image_thumb = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                try {
                    if (image_thumb != null) {
                        bit_thumb = MediaStore.Images.Media.getBitmap(resolver, Uri.parse(image_thumb));
                    } else {
                        Log.e("No Image Thumb", "--------------");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                SelectUser selectUser = new SelectUser();
                selectUser.setThumb(bit_thumb);
                selectUser.setName(name);
                selectUser.setPhone(phoneNumber);
                selectUser.setCheckedBox();
                selectUsers.add(selectUser);
            }
        } else {
            Log.e("Cursor close 1", "----------------");
        }
        //phones.close();
        return null;
    }

Log cat is

1-25 14:21:04.616 1502-1617/com.jamol.contacts E/art: Throwing OutOfMemoryError "Failed to allocate a 2073612 byte allocation with 347044 free bytes and 338KB until OOM"
    01-25 14:21:04.616 1502-1617/com.jamol.contacts D/skia: --- decoder->decode returned false
    01-25 14:21:04.616 1502-1617/com.jamol.contacts E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
      Process: com.jamol.contacts, PID: 1502
      java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:304)
          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
          at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
          at java.util.concurrent.FutureTask.run(FutureTask.java:242)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
          at java.lang.Thread.run(Thread.java:818)
       Caused by: java.lang.OutOfMemoryError: Failed to allocate a 2073612 byte allocation with 347044 free bytes and 338KB until OOM
          at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
          at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
          at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:863)
          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:839)
          at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:877)
          at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:860)
          at com.jamol.contacts.MainActivity$LoadContact.doInBackground(MainActivity.java:549)
          at com.jamol.contacts.MainActivity$LoadContact.doInBackground(MainActivity.java:527)
          at android.os.AsyncTask$2.call(AsyncTask.java:292)
          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
          at java.lang.Thread.run(Thread.java:818)
dev
  • 1,343
  • 2
  • 19
  • 40

1 Answers1

2

Actually, the thing what are you doing is very bad.
You are trying to store the whole bitmap in the SelectUser instance.
Of course, I don't know your task, but just imagine. For now you can select only one user. Later, you will decide to add ability for multi-select users. You will select 20 user and you want store their thumbnails in memory?
It's very bad. Please, instead of this, store in SelectUser only URL or URI of your image. And then, when you need to display it, use one of this library:

http://square.github.io/picasso/

https://github.com/bumptech/glide

https://github.com/nostra13/Android-Universal-Image-Loader

But, if you really need to get the bitmap, you should know, that your solution with MediaStore is outdated.

Use BitmapFactory instead.
You can read this answer:
https://stackoverflow.com/a/24135522/1796309

See the Android guide to handling large bitmaps.

Specifically this section:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

EDIT

In your case, you'll replace the decodeResource line with:

BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(),

null, options);

EDIT

Also, to load photos from contacts, you may check this answer:

https://stackoverflow.com/a/4240238/1796309

This works for me:

public static Bitmap loadContactPhoto(ContentResolver cr, long  id) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}
Community
  • 1
  • 1
Aleksandr
  • 4,906
  • 4
  • 32
  • 47