2011-12-07 14 views
2

カメラから撮影した画像をsdcardに保存する必要があるアプリがあります。カメラから撮影した画像を保存中にアンドロイドのjava.lang.OutOfMemoryError

camera.takePicture(myShutterCallback, myPictureCallback_RAW, 
         myPictureCallback_JPG); 

PictureCallback myPictureCallback_JPG = new PictureCallback() { 

     @Override 
     public void onPictureTaken(byte[] arg0, Camera arg1) { 

      Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, 
        arg0.length); 

      FileOutputStream outStream = null; 
      try { 
       outStream = new FileOutputStream(UploadedFilename); 
      } catch (FileNotFoundException e2) { 
       // TODO Auto-generated catch block 
       e2.printStackTrace(); 
      } 

      final Bitmap result = Bitmap.createScaledBitmap(bitmapPicture, 640, 
        480, false); 

この行のコード爆弾:ここ

コードである

ビットマップbitmapPicture = BitmapFactory.decodeByteArray(arg0に、0、 arg0.length)。

Exceptionクラスjava.lang.OutOfMemoryErrorを ソース方式BitmapFactory.nativeDecodeByteArray():

は、それは言います。

あなただけのSDカードに画像を保存したい場合は、ビットマップを作成する必要はありません

+0

内の他の興味深い設定がたくさんあることはあなたのイメージは、ビットマップを作成するには大きすぎるようです。 –

+0

ここをクリックしてください:http://stackoverflow.com/questions/1955410/bitmapfactory-oom-driving-me-nuts/ –

答えて

4

を助けてください。あなたが> = 640ピクセル幅が広い画像を取得したいとしましょう:

final int DESIRED_WIDTH = 640; 

// Set inJustDecodeBounds to get the current size of the image; does not 
// return a Bitmap 
final BitmapFactory.Options sizeOptions = new BitmapFactory.Options(); 
sizeOptions.inJustDecodeBounds = true; 
BitmapFactory.decodeByteArray(data, 0, data.length, sizeOptions); 
Log.d(TAG, "Bitmap is " + sizeOptions.outWidth + "x" 
      + sizeOptions.outHeight); 

// Now use the size to determine the ratio you want to shrink it 
final float widthSampling = sizeOptions.outWidth/DESIRED_WIDTH; 
sizeOptions.inJustDecodeBounds = false; 
// Note this drops the fractional portion, making it smaller 
sizeOptions.inSampleSize = (int) widthSampling; 
Log.d(TAG, "Sample size = " + sizeOptions.inSampleSize); 

// Scale by the smallest amount so that image is at least the desired 
// size in each direction 
final Bitmap result = BitmapFactory.decodeByteArray(data, 0, data.length, 
     sizeOptions); 

BitmapFactory.Options

関連する問題