2013-10-15 71 views
0

我的應用程序所做的只是從sdcard中獲取圖像並顯示在列表視圖中。當我從SD卡中選擇圖像時,它在ListView中正確顯示,但是當我從SD卡第二次選擇相同圖像時,應用程序崩潰。我想要什麼,如果第二次選擇相同的圖像,它應該提供烤麪包圖像已經存在。感謝您的幫助應用程序崩潰從SD卡中獲取圖片

保護無效onActivityResult(INT requestCode,INT resultCode爲,意圖數據){

super.onActivityResult(requestCode, resultCode, data); 
Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     Bitmap image=(BitmapFactory.decodeFile(picturePath)); 

     addattachmentsToListView(image); 

     cursor.close(); 

這裏是logcat的

10-16 00:53:34.432: E/dalvikvm-heap(5905): Out of memory on a 24023056-byte allocation. 
10-16 00:53:34.462: E/dalvikvm(5905): Out of memory: Heap Size=61575KB, Allocated=43470KB, Limit=65536KB 
10-16 00:53:34.462: E/dalvikvm(5905): Extra info: Footprint=61575KB, Allowed Footprint=61575KB, Trimmed=0KB 
10-16 00:53:34.462: E/AndroidRuntime(5905): FATAL EXCEPTION: main 
10-16 00:53:34.462: E/AndroidRuntime(5905): java.lang.OutOfMemoryError: (Heap Size=61575KB, Allocated=43470KB) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:658) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:730) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at com.example.androidufoneapp.CustomerRegistrationL0.onActivityResult(CustomerRegistrationL0.java:257) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.app.Activity.dispatchActivityResult(Activity.java:5275) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3618) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3672) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.os.Looper.loop(Looper.java:155) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at android.app.ActivityThread.main(ActivityThread.java:5454) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 
10-16 00:53:34.462: E/AndroidRuntime(5905):  at dalvik.system.NativeStart.main(Native Method) 
10-16 00:55:50.447: E/Trace(6372): error opening trace file: No such file or directory (2) 
10-16 00:56:08.376: E/dalvikvm-heap(6372): Out of memory on a 24023056-byte allocation. 
10-16 00:56:08.376: E/dalvikvm(6372): Out of memory: Heap Size=63559KB, Allocated=44806KB, Limit=65536KB 
10-16 00:56:08.376: E/dalvikvm(6372): Extra info: Footprint=63559KB, Allowed Footprint=63559KB, Trimmed=0KB 
10-16 00:56:08.386: E/AndroidRuntime(6372): FATAL EXCEPTION: main 
10-16 00:56:08.386: E/AndroidRuntime(6372): java.lang.OutOfMemoryError: (Heap Size=63559KB, Allocated=44806KB) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:658) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:730) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at com.example.androidufoneapp.CustomerRegistrationL0.onActivityResult(CustomerRegistrationL0.java:257) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.app.Activity.dispatchActivityResult(Activity.java:5275) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3618) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3672) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.os.Looper.loop(Looper.java:155) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at android.app.ActivityThread.main(ActivityThread.java:5454) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at java.lang.reflect.Method.invoke(Method.java:511) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796) 
10-16 00:56:08.386: E/AndroidRuntime(6372):  at dalvik.system.NativeStart.main(Native Method) 

回答

0

你需要做的是確保你是不是堅持一個位圖,並且您正在加載的任何位圖都不會佔用比目前更多的內存。在這些論壇中有很多資源涉及到這一點,這在Android中涵蓋。

Loading Bitmaps Efficiently

在這一點上這將是很好,如果他們開始與那些檢查包括包括內置的資源程序來檢索圖像(可讓說,在方法或東西標誌)。這個問題在很多地方都有涉及。

此外,您可以使用WeakReference來容納位圖以確保它被釋放。如果你想確定你以後的Toast,那麼你需要跟蹤這些圖像的名稱或其他東西;數據庫或本地內存的臨時解決方案,如果這是你在找什麼。

相關問題