2012-11-13 3 views
3

일부 기기 및 Android 기기에서 카메라로 사진을 찍을 때 문제가 있습니다. 예를 들어, 내 Xperia U v4.0.3에서 내 코드는 잘 작동하지만 다른 xperia U v2.3.3에서는 작동하지 않습니다 ...OutOfMemeryError 일부 기기에서 사진 인트로 찍기

이 오류에 대해 많이 읽었지만 해결할 수 없었습니다. ..

사진을 촬영하고 그것을 보여 내 코드 :

나는 내가 무엇을 할 수 있는지 모르겠다 ... 누군가가 나를 도울 수 있기를 바랍니다
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를 사용하여 확장 할 수 있습니다.

여기에 바로 할 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

문제의 원인은 기본 힙 크기 일 수 있습니다. 안드로이드 2.3 or or higher versions를 들어

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

: 2.2 안드로이드 이하 버전의

:

android:largeHeap="true" 

확인 문제의 원인이있는 경우는, 그에 따라 힙 증가를 증가시키는 방법이있다 .