2012-11-06 5 views
0

이 이미지를 표시하고 서버에 업로드하고 싶습니다.메모리 부족 오류 - 비트 맵 크기

카메라 앱을 사용하여 사진을 찍은 다음 사진의 파일 경로를 활동으로 되돌립니다. 장치간에 가져오고 가져 오는 이미지 크기가 일정하기 때문에 일부 전화에서는 메모리 부족 오류가 발생합니다.

휴대 전화의 메모리 제한 내에서 작업하면서 서버에 업로드 할 수있는 최대 이미지 크기를 가져올 수있는 방법은 무엇입니까?

코드는 다음과 같습니다

요청이 Aync를로드

GetBitmapTask GBT = new GetBitmapTask(dataType, path, 1920, 1920, loader); 
GBT.addAsyncTaskListener(new AsyncTaskDone() 
{ 
    @Override 
    public void loaded(Object resp) 
    {    
     crop.setImageBitmap((Bitmap)resp); 
     crop.setScaleType(ScaleType.MATRIX); 
    } 

    @Override 
    public void error() { 
    } 
}); 

GBT.execute(); 

움 오류

public class GetBitmapTask extends AsyncTask<Void, Integer, Bitmap> 
{ 

... 

@Override 
public Bitmap doInBackground(Void... params) 
{ 
    Bitmap r = null; 

    if (_dataType.equals("Unkown")) 
    { 
     Logger.e(getClass().getName(), "Error: Unkown File Type"); 
     return null; 
    } 
    else if (_dataType.equals("File")) 
    { 
     Options options = new Options();    
     options.inJustDecodeBounds = true; 

     //Logger.i(getClass().getSimpleName(), _path.substring(7)); 

     BitmapFactory.decodeFile(_path.substring(7), options); 

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

     Logger.i(getClass().getSimpleName(), 
       "height: " + options.outHeight + 
       "\nwidth: " + options.outWidth + 
       "\nmimetype: " + options.outMimeType + 
       "\nsample size: " + options.inSampleSize); 

     options.inJustDecodeBounds = false; 
     r = BitmapFactory.decodeFile(_path.substring(7), options); 

    } 
    else if (_dataType.equals("Http")) 
    { 
     r = _loader.downloadBitmap(_path, reqHeight); 

     Logger.i(getClass().getSimpleName(), "height: " + r.getHeight() + 
              "\nwidth: " + r.getWidth()); 
    } 

    return r; 
} 

public static int calculateInSampleSize(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; 

    while (height/inSampleSize > reqHeight || width/inSampleSize > reqWidth) 
    { 
     if (height > width) 
     { 
      inSampleSize = height/reqHeight; 
      if (((double)height % (double)reqHeight) != 0) 
      { 
       inSampleSize++; 
      } 
     } 
     else 
     { 
      inSampleSize = width/reqWidth; 
      if (((double)width % (double)reqWidth) != 0) 
      { 
       inSampleSize++; 
      } 
     } 
    } 
    return inSampleSize; 
} 
} 
을 던졌습니다 비동기 작업 당신은 카메라에게 당신이 원하는 파일에 URI를 가리키는 줄 수

답변

1

이미지를 다음에 저장하십시오 :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, mDefaultPhotoUri); // set path to image file 

그러면 해당 파일을 서버에 업로드 할 수 있으므로 전체 비트 맵 크기를 갖게됩니다. 반면에, UI에서 비트 맵 1920x1920 (또는 비슷한)을 디코딩하고 표시 할 필요가 없습니다 (많은 장치에서). 너무 큽니다.

희망이 도움이됩니다.

+0

오류가 비트 맵 작업보다 코드에 더 많이 포함되어 있음을 알게되었지만 1920X1920 이미지를 표시 할 필요가없는 것이 맞습니다. 감사! –