这里我无法在bitmap中分配内存。这里我用logcat附加了代码。任何人都可以为它提供解决方案。我已声明位图为空,即使我得到相同的错误。
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
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)
答案 0 :(得分:2)
实际上,你做的事情非常糟糕
您试图将整个位图存储在SelectUser
实例中
当然,我不知道你的任务,但想象一下。目前,您只能选择一个用户。稍后,您将决定为多选用户添加功能。您将选择20个用户,并且您希望将缩略图存储在内存中?
这很糟糕。请代替此,仅在SelectUser
存储图片的网址或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
请参阅handling large bitmaps的Android指南。
特别是本节:
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(),
null,options);
另外,要从联系人加载照片,您可以查看以下答案:
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); }