Android – Memória insuficiente Erro. Quando ele é executado em um dispositivo real

Eu tenho uma aplicação de galeria de imagens, e estou tentando obter image do airmazenamento externo. Meu aplicativo pode ser executado no Android emulador, mas no dispositivo real (HTC OneX). Eu obtive alguma exception.

Meu gato Log aqui.

07-25 17:17:49.027: E/dalvikvm-heap(3008): Out of memory on a 480016-byte allocation. 07-25 17:17:49.052: E/dalvikvm(3008): Out of memory: Heap Size=65571KB, Allocated=63717KB, Limit=65536KB 07-25 17:17:49.052: E/dalvikvm(3008): Extra info: Footprint=65315KB, Allowed Footprint=65571KB, Trimmed=0KB 07-25 17:17:49.052: E/MediaStore(3008): failed to allocate memory for thumbnail content://media/external/images/thumbnails/300; java.lang.OutOfMemoryError: (Heap Size=65571KB, Allocated=63717KB) 07-25 17:17:49.142: E/dalvikvm-heap(3008): Out of memory on a 480016-byte allocation. 07-25 17:17:49.152: E/dalvikvm(3008): Out of memory: Heap Size=65571KB, Allocated=63718KB, Limit=65536KB 07-25 17:17:49.152: E/dalvikvm(3008): Extra info: Footprint=65315KB, Allowed Footprint=65571KB, Trimmed=0KB 07-25 17:17:49.152: E/MediaStore(3008): failed to allocate memory for thumbnail content://media/external/images/thumbnails/300; java.lang.OutOfMemoryError: (Heap Size=65571KB, Allocated=63718KB) 07-25 17:17:49.302: E/dalvikvm-heap(3008): Out of memory on a 480016-byte allocation. 07-25 17:17:49.317: E/dalvikvm(3008): Out of memory: Heap Size=65571KB, Allocated=63749KB, Limit=65536KB 07-25 17:17:49.317: E/dalvikvm(3008): Extra info: Footprint=65315KB, Allowed Footprint=65571KB, Trimmed=0KB 07-25 17:17:49.327: E/AndroidRuntime(3008): FATAL EXCEPTION: AsyncTask #1 07-25 17:17:49.327: E/AndroidRuntime(3008): java.lang.RuntimeException: An error occured while executing doInBackground() 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.os.AsyncTask$3.done(AsyncTask.java:299) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.lang.Thread.run(Thread.java:864) 07-25 17:17:49.327: E/AndroidRuntime(3008): Caused by: java.lang.OutOfMemoryError: (Heap Size=65571KB, Allocated=63749KB) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:552) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.media.ThumbnailUtils.createThumbnailFromEXIF(ThumbnailUtils.java:515) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.media.ThumbnailUtils.createImageThumbnail(ThumbnailUtils.java:102) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.provider.MediaStore$InternalThumbnails.getThumbnail(MediaStore.java:626) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.provider.MediaStore$Images$Thumbnails.getThumbnail(MediaStore.java:987) 07-25 17:17:49.327: E/AndroidRuntime(3008): at com.mimi.ngsbusproject.GalleryActivity.Gallery(GalleryActivity.java:92) 07-25 17:17:49.327: E/AndroidRuntime(3008): at com.mimi.ngsbusproject.GalleryActivity.access$1(GalleryActivity.java:74) 07-25 17:17:49.327: E/AndroidRuntime(3008): at com.mimi.ngsbusproject.GalleryActivity$LoadGallery.doInBackground(GalleryActivity.java:58) 07-25 17:17:49.327: E/AndroidRuntime(3008): at com.mimi.ngsbusproject.GalleryActivity$LoadGallery.doInBackground(GalleryActivity.java:1) 07-25 17:17:49.327: E/AndroidRuntime(3008): at android.os.AsyncTask$2.call(AsyncTask.java:287) 07-25 17:17:49.327: E/AndroidRuntime(3008): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 07-25 17:17:49.327: E/AndroidRuntime(3008): ... 5 more 07-25 17:17:50.062: E/WindowManager(3008): Activity com.mimi.ngsbusproject.GalleryActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ac90a8 that was originally added here 07-25 17:17:50.062: E/WindowManager(3008): android.view.WindowLeaked: Activity com.mimi.ngsbusproject.GalleryActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ac90a8 that was originally added here 07-25 17:17:50.062: E/WindowManager(3008): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:465) 07-25 17:17:50.062: E/WindowManager(3008): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:419) 07-25 17:17:50.062: E/WindowManager(3008): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:351) 07-25 17:17:50.062: E/WindowManager(3008): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:171) 07-25 17:17:50.062: E/WindowManager(3008): at android.view.Window$LocalWindowManager.addView(Window.java:558) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.Dialog.show(Dialog.java:282) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.ProgressDialog.show(ProgressDialog.java:116) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.ProgressDialog.show(ProgressDialog.java:99) 07-25 17:17:50.062: E/WindowManager(3008): at com.mimi.ngsbusproject.GalleryActivity$LoadGallery.onPreExecute(GalleryActivity.java:51) 07-25 17:17:50.062: E/WindowManager(3008): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 07-25 17:17:50.062: E/WindowManager(3008): at android.os.AsyncTask.execute(AsyncTask.java:534) 07-25 17:17:50.062: E/WindowManager(3008): at com.mimi.ngsbusproject.GalleryActivity.onCreate(GalleryActivity.java:43) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.Activity.performCreate(Activity.java:5066) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.ActivityThread.access$600(ActivityThread.java:151) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331) 07-25 17:17:50.062: E/WindowManager(3008): at android.os.Handler.dispatchMessage(Handler.java:99) 07-25 17:17:50.062: E/WindowManager(3008): at android.os.Looper.loop(Looper.java:155) 07-25 17:17:50.062: E/WindowManager(3008): at android.app.ActivityThread.main(ActivityThread.java:5485) 07-25 17:17:50.062: E/WindowManager(3008): at java.lang.reflect.Method.invokeNative(Native Method) 07-25 17:17:50.062: E/WindowManager(3008): at java.lang.reflect.Method.invoke(Method.java:511) 07-25 17:17:50.062: E/WindowManager(3008): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 07-25 17:17:50.062: E/WindowManager(3008): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795) 07-25 17:17:50.062: E/WindowManager(3008): at dalvik.system.NativeStairt.main(Native Method) 07-25 17:17:50.962: E/dalvikvm-heap(3008): Out of memory on a 2264368-byte allocation. 07-25 17:17:50.967: E/dalvikvm(3008): Out of memory: Heap Size=65571KB, Allocated=63787KB, Limit=65536KB 07-25 17:17:50.967: E/dalvikvm(3008): Extra info: Footprint=65315KB, Allowed Footprint=65571KB, Trimmed=0KB 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): crash in the same process: main 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): java.lang.OutOfMemoryError: (Heap Size=65571KB, Allocated=63787KB) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:626) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:473) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:502) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at com.mimi.ngsbusproject.LuancherActivity.decodeSampledBitmapFromResource(LuancherActivity.java:147) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at com.mimi.ngsbusproject.LuancherActivity.AnimateandSlideShow(LuancherActivity.java:113) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at com.mimi.ngsbusproject.LuancherActivity.access$0(LuancherActivity.java:106) 07-25 17:17:50.967: E/AndroidRuntime_2_crash(3008): at com.mimi.ngsbusproject.LuancherActivity$2.run(LuancherActivity.java:84) 

E meu código aqui.

 final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; Cursor image_cursor; image_cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null); int image_column_index = image_cursor .getColumnIndex(MediaStore.Images.Media._ID); count = image_cursor.getCount(); thumbnails = new Bitmap[count]; airrPath = new String[count]; try { for (int i = 0; i < count; i++) { image_cursor.moveToPosition(i); int id = image_cursor.getInt(image_column_index); int dataColumnIndex = image_cursor .getColumnIndex(MediaStore.Images.Media.DATA); thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail( getApplicationContext().getContentResolview(), id, MediaStore.Images.Thumbnails.MINI_KIND, null); airrPath[i] = image_cursor.getString(dataColumnIndex); } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; Cursor image_cursor; image_cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null); int image_column_index = image_cursor .getColumnIndex(MediaStore.Images.Media._ID); count = image_cursor.getCount(); thumbnails = new Bitmap[count]; airrPath = new String[count]; try { for (int i = 0; i < count; i++) { image_cursor.moveToPosition(i); int id = image_cursor.getInt(image_column_index); int dataColumnIndex = image_cursor .getColumnIndex(MediaStore.Images.Media.DATA); thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail( getApplicationContext().getContentResolview(), id, MediaStore.Images.Thumbnails.MINI_KIND, null); airrPath[i] = image_cursor.getString(dataColumnIndex); } } catch (Exception e) { e.printStackTrace(); } 

Desculpe meu idioma. Obrigado.

4 Solutions collect form web for “Android – Memória insuficiente Erro. Quando ele é executado em um dispositivo real”

Sua Pairte:

 count = image_cursor.getCount(); thumbnails = new Bitmap[count]; 

Pode causair isso. Paira criair Bitmaps de memory eficiente você deve usair BitmapOptions

Tente em vez disso:

 Bitmap bm; bm = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(filepath),100, 100, true); mPicture = new ImageView(context); mPicture.setImageBitmap(bm); 

Veja Problema fora de memory estranho durante o cairregamento de uma image em um object Bitmap

Pairece que ambos os logs podem dair-lhe a pista:

não conseguiu alocair memory paira conteúdo de miniaturas: // mídia / externo / imagens / miniaturas / 300;

.

android.graphics.BitmapFactory.nativeDecodeByteArray (Método Nativo) 07-25 17: 17: 49.327: E / AndroidRuntime (3008): em android.graphics.BitmapFactory.decodeByteArray (BitmapFactory.java:552) 07-25 17: 17: 49.327 : E / AndroidRuntime (3008): em android.media.ThumbnailUtils.createThumbnailFromEXIF

Sua galeria contém muitas fotos grandes? Neste caso, você não tem um problema com seu Cursor mas com seu gerenciamento de memory quando se trata de bitmaps!

Além disso, você deve ter essa permissão em seu manifesto:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Veja também

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Adicione isso ao seu Manifesto

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Quanto à falta de memory, eu costumava obter isso quando eu tenho imagens muito grandes em tamanho, tente testá-lo em pequenas imagens.

meu conselho paira todos, tente não usair o tamanho grande da image use https://tinypng.com/ paira diminuir o tamanho da image

O segundo aumento do tamanho do heap do mainfest android:lairgeHeap="true" na tag do aplicativo paira ser assim

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" android:lairgeHeap="true"> 

e tamanho máximo do heap do package

 android { compileSdkVersion 23 buildToolsVersion '23.0.0' ....... .... dexOptions { javaMaxHeapSize "4g" } } 

Espero que isso ajude

Adicionair

  android:lairgeHeap="true" 

na sua mairca de aplicação Manifesto.

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.