2012-11-13 70 views
3

我在某些設備和Android版本中使用相機拍照時出現問題。例如,在我的Xperia U v4.0.3中,我的代碼運行良好,但在另一個xperia U v2.3.3中不起作用...OutOfMemeryError在某些設備中拍照android

我讀了很多關於此錯誤的信息,但我無法修復它。 ..

我的代碼拍照,並顯示:

public void callCamera(){ 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg"); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, 
Uri.fromFile(photo)); 
imageUri = Uri.fromFile(photo); 
startActivityForResult(intent, SELECT_CAMERA); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (requestCode == SELECT_CAMERA) { 


       Uri selectedImage = imageUri; 
       getContentResolver().notifyChange(selectedImage, null); 

       ContentResolver cr = getContentResolver(); 

       try { 

        imageSelected = android.provider.MediaStore.Images.Media 
        .getBitmap(cr, selectedImage); 
        // I tried to read the image created for the camera in the Sd 
        //But I've got the same error... 
        //imageSelected = Utils.readImageSD(this, selectedImage.getPath()); 

        imageSelected = Utils.rotateImage(Utils.scaleBitmap(imageSelected, 160, 160)); 
        ivImageLoad.setImageBitmap(imageSelected); 

       } catch (Exception e) { 
        Utils.showToast(getApplicationContext(), R.string.errorLoad); 
        Log.e("Camera", e.toString()); 
       } 

      } 

} 

我希望有人能幫助我......我不知道我能做些什麼....

謝謝提前。

問候。

+0

完美髮布日誌貓在這裏 – RajeshVijayakumar

回答

4

它看起來像只想顯示一個160x160位圖,但是在這一行上,您正在讀取整個位圖,然後再縮放它。

imageSelected = android.provider.MediaStore.Images.Media(cr, selectedImage); 

您可以通過使用inJustDecodeBounds避免整個有史以來位圖加載到內存中,然後就可以使用inSampleSize縮放。

這裏是從Android系列的一些示例代碼上Loading Large Bitmaps Efficiently

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

public static Bitmap decodeSampledBitmapFromUri(Uri uri, 
     int reqWidth, int reqHeight) { 


    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), options); 

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

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); 
} 

然後讓你的160x160的圖片你會只是做:

ivImageLoad.setImageBitmap(decodeSampledBitmapFromUri(selectedImage, 100, 100)); 
+0

作品!非常感謝! – user1819600

0

也許問題的根源是默認堆大小。有辦法來增加它:

對於Android 2.2或更低版本:

dalvik.system.VMRuntime.getRuntime().setMinimumHeapSize(sizeInBytes); 

對於Android的2.3 or or higher versions

android:largeHeap="true" 

檢查問題的根源是這樣的,相應增加堆。

相關問題