2012-10-19 89 views
1

在我的應用程序中的錯誤我有一個選項,以顯示在SD卡中的圖像,用戶可以選擇一個圖像,它會被加載我的下一個活動的圖像視圖。android從sd卡載入圖像到imageview創建內存不足三星SIII

我已經在我的兩個設備(如三星Ace和三星Galaxy S3)中測試了該應用程序。

在2.3版本的三星Ace中,我可以選擇圖像並從onActivity結果中調用下一個Activity,圖像在Imageview中加載,在這裏我可以做到n次,(其他我使用的選項是通過相機捕獲和加載在下一個活動,這裏也是這個過程是好的)

當我在第一次嘗試在三星Galaxy S3,版本4.2的相同過程,我可以選擇或捕獲通過相機並在imageview中加載它。當我試圖在第二次應用程序被墜毀,說

E/dalvikvm-heap(7058): Out of memory on a 31961104-byte allocation. 
E/AndroidRuntime(7058): FATAL EXCEPTION: main 
E/AndroidRuntime(7058): java.lang.OutOfMemoryError 
E/AndroidRuntime(7058): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
E/AndroidRuntime(7058): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587) 
E/AndroidRuntime(7058): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389) 
E/AndroidRuntime(7058): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418) 
E/AndroidRuntime(7058): at com.tribal.pages.app.UploadPhoto$ShowCapturedImage.onPostExecute(UploadPhoto.java:442) 
E/AndroidRuntime(7058): at com.tribal.pages.app.UploadPhoto$ShowCapturedImage.onPostExecute(UploadPhoto.java:1) 
E/AndroidRuntime(7058): at android.os.AsyncTask.finish(AsyncTask.java:602) 
E/AndroidRuntime(7058): at android.os.AsyncTask.access$600(AsyncTask.java:156) 
E/AndroidRuntime(7058): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 
E/AndroidRuntime(7058): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(7058): at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime(7058): at android.app.ActivityThread.main(ActivityThread.java:4517) 
E/AndroidRuntime(7058): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(7058): at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime(7058): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 
E/AndroidRuntime(7058): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 
E/AndroidRuntime(7058): at dalvik.system.NativeStart.main(Native Method) 
E/android.os.Debug(2098): [email protected] > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error 
E/widget (2468): [MSC_HERO_Accu]>>> SM:399 [0:0] IR : false, IPR : false 
E/Launcher(2468): Error finding setting, default accessibility to not found: accessibility_enabled 
E/widget (2468): [MSC_HERO_Accu]>>> SM:399 [0:0] IR : false, IPR : false 

我使用以下2種方法來加載在ImageView的 方法所捕獲的圖像或選擇的圖像試圖1

Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(Appconstants._uri)); 
         bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
ImageView uplaod_image.setImageBitmap(bitmap); 

方法2

Bitmap bitmap = BitmapFactory.decodeFile(Appconstants.f); 
ImageView uplaod_image.setImageBitmap(bitmap); 

但每次應用程序崩潰說上面的錯誤,如何解決這個....

+0

那張圖片有多大? – xfx

+0

你的意思是大小 - 所拍攝的圖像大約爲1.2MB到1.7MB ..... –

回答

2

您在行爲中看到的差異可能是由於兩部手機上的圖像尺寸不同所致。

你可能想打電話,

upload_image.setImageDrawable(null); 

清除ImageView的(或使用的方式之一詳述here),試圖加載另一個圖像之前,使該內存清理和收集到的垃圾收集器在後續分配之前,並防止出現Out of memory錯誤。

1

嘗試加載到該按設備具有與屏幕尺寸的內存之前縮放圖像,

您可以使用BitmapFactory選項inJustDecodeBounds = true其解碼到內存之前得到的位圖的大小。

獲取的高度和寬度,

int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 

然後你就可以與所要求的高度和圖像按您的要求的寬度縮放,

int inSampleSize = Math.min(imageWidth /reqWidth,imageHeight /reqHeight); 

然後終於應用inSampleSize規模位圖。對於,進一步的細節檢查我的答案here

相關問題