2014-07-16 3 views
1

android-crop 라이브러리를 사용 중입니다. 사용자가 큰 이미지 (전화 카메라의 이미지)에서 자르려고하면 메모리 부족으로 인해 절망적입니다.비트 맵 압축시 OutOfMemoryError

결과 비트 맵을 압축하려고했지만 비트 맵을 압축하려고 할 때 여전히 OutOfMemoryError이 표시됩니다. 여기에 내 코드가 간다 : 내가 뭘 잘못하고 있고/또는 어떻게 메모리 오류가 발생하지 않도록 할 수 있는지 알려주시겠습니까? 나는이 문제를과 같이 해결할 수있는 게이브의 관점에

private void handleCrop(int resultCode, Intent result) { 
    if (resultCode == RESULT_OK) { 

     if (mBitmap != null) { 
      mBitmap.recycle(); 
      mBitmap = null; 
     } 

     Bitmap temp; //temporary bitmap 

     try { 
      temp = MediaStore.Images.Media.getBitmap(getContentResolver() 
        , Crop.getOutput(result)); //load image from URI 

      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      temp.compress(Bitmap.CompressFormat.JPEG, 60, out); 
      mBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); 

      out.close(); 
      temp.recycle(); 
      temp = null; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Toast.makeText(this, "error loading photo. please try with another photo" 
        , Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     if (mBitmap != null) { 
      mImageViewProfilePhoto.setImageBitmap(mBitmap); 
      enableFinalization(); 
     } 

    } 
} 

감사 :

private void handleCrop(int resultCode, Intent result) { 
    if (resultCode == RESULT_OK) { 

     if (mBitmap != null) { 
      mBitmap.recycle(); 
      mBitmap = null; 
     } 

     try { 
      mBitmap = MediaStore.Images.Media.getBitmap(getContentResolver() 
        , Crop.getOutput(result)); //load image from URI 

      File tempImageFile = new File(mContext.getFilesDir().getAbsolutePath() 
        , "temp_1311_14_hahahah_lol_WTF_shit_1414.bin"); 

      FileOutputStream out = new FileOutputStream (tempImageFile); 
      mBitmap.compress(Bitmap.CompressFormat.JPEG, 30, out); 
      mBitmap.recycle(); 
      mBitmap = null; 

      mBitmap = BitmapFactory.decodeFile(tempImageFile.getPath()); 
      boolean deleted = tempImageFile.delete(); 
      out.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Toast.makeText(this, "error loading photo. please try with another photo" 
        , Toast.LENGTH_SHORT).show(); 
      return; 
     } 

     if (mBitmap != null) { 
      mImageViewProfilePhoto.setImageBitmap(mBitmap); 
      enableFinalization(); 
     } 

    } else if (resultCode == Crop.RESULT_ERROR) { 
     Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show(); 
     disableFinalization(); 
    } 
} 

은 다른 사람을 위해 도움이 될 것입니다 바랍니다.

답변

1

문제는 응용 프로그램의 다른 위치에 있지 않으며 항상 OOM과 관련 될 수 있다고 가정합니다. 이미지가 큰 경우 기본적으로 3 개의 복사본을 만듭니다. 첫 번째는 원본이며 두 번째는 ByteArrayOutputStream에 있고 세 번째는 decodeStream에 있습니다. 이미지가 큰 경우 (10MB 이상) 혼자서 문제가 발생할 수 있습니다. 디스크 기반 출력 스트림을 사용하여 문제가 발생하는지 확인하는 것이 좋습니다. 또한 temp.recycle 다음에 decodeStream 호출을 넣으면 한 번에 2 개의 전체 복사본을 가질 수 없습니다.

+0

감사합니다. 임시 비트 맵을 제거하고 파일 스트림 insted를 사용하여 문제가 해결되었습니다! –

관련 문제