2012-10-02 2 views
1

갤러리 또는 카메라의 이미지로 작동하는 작은 앱을 만들었습니다.메모리에로드하기 전에 Android 이미지 크기를 확인하십시오.

모두 정상적으로 작동하지만 작은 화면과 작은 메모리 크기 (HTC Desire)가있는 장치에서 다른 휴대 전화에서 전체 크기로 다운로드 한 이미지가 있으며 크기가 훨씬 큽니다 (해당 휴대 전화의 8MP 카메라).

내 작은 카메라 거대한 이미지를로드하려고하면 바로 충돌합니다.

그래서 어떤 종류의 검사를 구현하고 해당 이미지를 축소하는 방법은 있지만 올바르게로드합니까?

이미지를로드 한 후에 크기를 조정하지만 충돌이 발생하기 전에 수행해야하는 작업입니다.

Tnx.

  InputStream in = null; 
      try { 
       in = getContentResolver().openInputStream(data.getData()); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      // get picture size. 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(in, null, options); 
      try { 
       in.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      // resize the picture for memory. 
      int screenH = getResources().getDisplayMetrics().heightPixels; //800 
      int screenW = getResources().getDisplayMetrics().widthPixels; //480 
      int width = options.outWidth/screenW; 
      int height = options.outHeight/screenH; 

      Log.w("Screen Width", Integer.toString(width)); 
      Log.w("Screen Height", Integer.toString(height)); 

      int sampleSize = Math.max(width, height); 
      options.inSampleSize = sampleSize; 
      options.inJustDecodeBounds = false; 
      try { 
       in = getContentResolver().openInputStream(data.getData()); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      // convert to bitmap with declared size. 
      Globals.INSTANCE.imageBmp = BitmapFactory.decodeStream(in, null, options); 
      try { 
       in.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

답변

1

당신은 당신이 디코드없이 이미지의 경계를 디코딩 할 수

inJustDecodeBounds = true 

inJustDecodeBounds 설정을 메모리에 비트 맵을로드 피할 수 있습니다. 비트 맵의 ​​heightwidth이 있으면이를 사용하여 다운 샘플링 할 수 있습니다.

inSampleSize

는 문서가 유지 같이

값> 1로 설정된 경우

는, 메모리를 절약하기 위해 더 작은 이미지를 반환 일본어 이미지를 서브 샘플링하는 디코더를 요구한다.

int tmpWidth = bitmapWidth; 
int tmpHeight = bitmapHeigth; 
int requiredSize = ... 
while (true) { 
if (tmpWidth/2 < requiredSize 
    || tmpHeight/2 < requiredSize) 
     break; 
    tmpWidth /= 2; 
    tmpHeight /= 2; 
    ratio *= 2; 
} 

편집 : 필요한 32 비트 Bitmap 메모리를 위해 내가 답변을받지 못했습니다, width * height * 4

+0

내가 인정해야합니다. 내 질문을 코드로 편집합니다. – Balkyto

+0

그래서 나는 그것을 어떤 스크린 에나 넣고, 스크린 크기와 아마도 스크린 밀도를 읽으려고 노력하지만, 여전히 크다. 비율/밀도와 관련하여 뭔가 빠져 있어야합니다. – Balkyto

+0

빅 (800 또는 480)으로 생각합니다. inSampleSize = 8 설정을 시도하십시오. – Blackbelt

관련 문제