2013-07-26 18 views
3

我必須獲取圖庫中圖像的圖像路徑,將其保存爲列表,不幸的是,如果選擇的圖像太大,以下代碼會崩潰應用程序(以及我的警報嘗試catch塊不被查看)。如果我選擇大圖像,OnActivityResult崩潰

private void openImage() { 

     try{ 
      Intent i = new Intent(Intent.ACTION_GET_CONTENT); 
      i.setType("image/*"); 
      startActivityForResult(i, FILE_REQ_CODE); 
      } catch (RuntimeException e) { 
          e.printStackTrace(); 
       Alerts.TooLargeImage(LoadImage.this); 
      } 

    } 

    protected void onActivityResult(int requestCode, int resultCode, 
      Intent intentData) { 
     try{ 
      Uri tmp = intentData.getData(); 
      String path = tmp.toString(); 
      imagePathList.add(path); 
      preview.setImageURI(tmp); 
      FileArchiveManager.saveImagePath(imagePathList); 
      super.onActivityResult(requestCode, resultCode, intentData); 
      } catch (RuntimeException e) { 
          e.printStackTrace(); 
       Alerts.TooLargeImage(LoadImage.this); 
      } 
    } 

錯誤日誌

java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=8135KB, Allocated=3718KB, Bitmap Size=11707KB) 
    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:694) 
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:494) 
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697) 
    at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657) 
    at android.widget.ImageView.resolveUri(ImageView.java:592) 
    at android.widget.ImageView.setImageURI(ImageView.java:313) 
    at com.myapp.LoadImage.onActivityResult(LoadImage.java:131) 
    at android.app.Activity.dispatchActivityResult(Activity.java:4108) 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3016) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3072) 
    at android.app.ActivityThread.access$2000(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1084) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:150) 
    at android.app.ActivityThread.main(ActivityThread.java:4385) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
    at dalvik.system.NativeStart.main(Native Method) 

如何解決這個問題?

回答

1

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

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
    int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100)); 

使用BitmapFactory.decodeResource()BitmapFactory.Options.inSampleSize將讓你的位圖的下采樣版本加載到RAM(因爲你現實就不需要這麼大的圖像顯示),而不會造成java.lang.OutofMemoryError

+0

感謝您的回答,但我不知道如何在我的方法 – AndreaF

+0

中使用您的代碼與java.io.EOFException的應用程序崩潰,並沒有顯示圖庫,如果我嘗試使用setImageBitmap(decodeSampledBitmapFromResource(getResources (),R.id.myimage,100,100));在onActivityResult – AndreaF

+0

你能給我更多關於EOFException的細節嗎? –