2016-01-25 77 views
1

在這裏,我無法分配位圖中的內存。在這裏我已經附加代碼與logcat。任何人都可以爲它提供解決方案。我已經宣佈位圖爲null,即使我得到相同的錯誤。未能分配內存android

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; 
    } 

登錄貓

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) 

回答

1

其實,你在做事情是非常糟糕的。
您正在嘗試將整個位圖存儲在SelectUser實例中。
當然,我不知道你的任務,但只是想象。現在你只能選擇一個用戶。稍後,您將決定爲多選用戶添加功能。您將選擇20位用戶,並且您希望將縮略圖存儲在內存中?
這很糟糕。請替代此,請僅在SelectUser中存儲圖像的URL或URI。然後,當你需要顯示它,使用這個庫之一:

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

https://github.com/bumptech/glide

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

但是,如果你真的需要得到位圖,你應該知道,您的解決方案MediaStore已過時。使用BitmapFactory代替。
你可以閱讀這個答案:
https://stackoverflow.com/a/24135522/1796309

看到Android的指導handling large bitmaps

具體來說本節:

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; 

編輯

在你的情況,你會替換爲decodeResource行:

BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), 

空,選項);

編輯

另外,從接觸加載照片,你可以檢查這個答案:

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

這個工作對我來說:

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); 
}