2011-01-31 10 views
2

我在android上選取應用程序中的圖像,我不知道爲什麼,有時我得到了一個例外,我認爲它總是選擇一個超過400或500 kb的圖像,但我不確定。爲什麼?因爲當我選擇100 kb的小圖像時,我沒有得到異常,並且當我得到高KB或MB圖像時,它會與java.lang.OutOfMemoryError: bitmap size exceeds VM budget一起崩潰。 但非常重要:它在模擬器上崩潰,但也在我的手機上崩潰,不是模擬器的故障。從圖庫(SD卡)中挑選圖片以用於我的應用... java.lang.OutOfMemoryError:位圖大小超過虛擬機預算

這是我的代碼:

changeImageButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Intent i = new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
       startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 
      } 
     }); 



protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

     switch(requestCode) { 
     case 1: 
     { 
      setResult(1); 
      finish();  
     } 
     case ACTIVITY_SELECT_IMAGE: 
      if(resultCode == RESULT_OK){ 
       Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex); 
       cursor.close(); 

       selectedPhoto = BitmapFactory.decodeFile(filePath); 
       //profileImage.setImageBitmap(selectedPhoto); 
       profileImage.setImageBitmap(Bitmap.createScaledBitmap(selectedPhoto, 80, 80, false)); 
      } 
     } 
    } 

profileImage是我佈局的ImageView的。我用縮放butmap的圖像resice到80×80

請給我一些幫助,這個例外,我需要去解決它

這是個例外:

01-31 12:38:37.531: ERROR/AndroidRuntime(1025): Uncaught handler: thread main exiting due to uncaught exception 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:375) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:171) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:196) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at com.GPSLoc.Configuration.onActivityResult(Configuration.java:253) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.app.Activity.dispatchActivityResult(Activity.java:3595) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3001) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3047) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.app.ActivityThread.access$2300(ActivityThread.java:112) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.os.Looper.loop(Looper.java:123) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at android.app.ActivityThread.main(ActivityThread.java:3948) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at java.lang.reflect.Method.invokeNative(Native Method) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at java.lang.reflect.Method.invoke(Method.java:521) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 
    01-31 12:38:37.552: ERROR/AndroidRuntime(1025):  at dalvik.system.NativeStart.main(Native Method) 
+0

我面臨同樣的問題,這是什麼解決方案@ AndroidUser99 – Crishnan 2014-02-17 12:05:28

回答

1

Bitmap.createScaledBitmap方法只要我研究過內存消耗高點併產生錯誤,但我並不完全確定它。無論如何,這是JAVA API,你不能更深入。

你可以做下:

//create a Drawable with your image as parameter 
BitmapDrawable d= new BitmapDrawable(youBitmap); 
//define bounds for your drawable  
int left =0; 
int top = 0; 
int right=80; 
int bottom=80; 

Rect r = new Rect(left,top,right,bottom); 
//set the new bounds to your drawable  
d.setBounds(r); 
//set the drawable as view of your image view 
profileImage.setImageDrawable(d); 

我還沒有測試此代碼,但它應該工作。

+0

不,它仍然崩潰,同樣的例外:S – NullPointerException 2011-01-31 13:55:07

相關問題