2012-02-29 47 views
0

これは私のコードです。画像貼り付けエラー、メモリー不足

case RESULT_MEMORY_SELECT:  //SELECTING IMAGE FROM SD CARD 
       Uri photoUri = data.getData(); 
       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
       Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null); 
        if (cursor.moveToFirst()) 
        { 
         int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
         String filePath = cursor.getString(columnIndex); 
         cursor.close(); 
         Bitmap imageReturned = BitmapFactory.decodeFile(filePath); 
         showViewOfReceiptInLayout(imageReturned); 
        } 
        break; 


public void showViewOfReceiptInLayout(Bitmap imageBitmap) 
     { 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      imageSelected = baos.toByteArray(); // imageSelected is byteArray and i am storing this byte array in Database 
      imageBitmap = Bitmap.createScaledBitmap(imageBitmap, 72, 72, false); 
      image.setVisibility(View.VISIBLE); // For Visible 
      image.setImageBitmap(imageBitmap); 


02-29 10:30:44.496: E/AndroidRuntime(7682): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
02-29 10:30:44.496: E/AndroidRuntime(7682):  at android.graphics.Bitmap.nativeCreate(Native Method) 
02-29 10:30:44.496: E/AndroidRuntime(7682):  at android.graphics.Bitmap.createBitmap(Bitmap.java:468) 
02-29 10:30:44.496: E/AndroidRuntime(7682):  at android.graphics.Bitmap.createBitmap(Bitmap.java:435) 

誰でもこのエラーを修正する方法を教えてください。

答えて

0

以下は、メモリ不足を避けるためにイメージのサイズを縮小する必要があるソリューションです。デコード画像の前に、あなたが使用することをいけない場合、オブジェクト参照を解放し、いくつかのメモリを解放する必要があるので、あなたのアプリケーションのメモリ使用量は、デコード画像の前に大きい場合

:最初

BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inSampleSize = 8; 
     options.inJustDecodeBounds = true; 

     Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options); 

     final int REQUIRED_SIZE=70; 
     int width_tmp=options.outWidth, height_tmp=options.outHeight; 
     int scale=1; 
     while(true){ 
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
       break; 
      width_tmp/=2; 
      height_tmp/=2; 
      scale*=2; 
     } 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     Bitmap btm=BitmapFactory.decodeStream(is, null, o2); 
     img_t.setImageBitmap(btm); 
+0

品質を失うことはありませんか?私は、データベースに保存する予定の画像のサイズと画質を減らしたくありません。 –

+0

それはあなたの意見とは別のオプションがありません。 – Maneesh

0

は、あなたがOOMの理由を見つける必要がありますオブジェクト。

使用できる

adb shell dumpsys meminfo your_package_nameをメモリuseage

を表示するには、アプリケーションのメモリ使用量はデコード画像の前に小さい場合にこれが起こる場合、OOMは、デコード画像が発生している、提案:

1.ifデコード、BitmapFactory.Optionを供給し、オプションがOutOfMemroyError

をキャッチし、デコード方法の周りにtryブロックを2.Make画像

を復号化するために、小さなメモリを使用するinSampleSizeを持っているとき、あなたのイメージは、大きいです

関連する問題