2012-08-27 4 views
0

갤러리에서 이미지를 가져오고 있습니다 (의도별로).비트 맵 메모리 부족

985120-byte external allocation too large for this process. 
Out of memory: Heap Size=4871KB, Allocated=2472KB, Bitmap Size=19677KB 
VM won't let us allocate 985120 bytes 

나는 이미지를 얻을 어디에 내 코드입니다 :

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    .... 
    mBitmap = Media.getBitmap(this.getContentResolver(), data.getData()); 
    ... 
} 

내가 그것을 어떻게 해결할 수
는이 오류가있어?

-------- UPDATE는 ---------

은 내가 (HTC 사진 설치) 사전에 존재하지 않는 이미지를 선택하면 나는이 오류가 나타났습니다. 카메라에서 선택한 이미지를 선택하면 모두 정상적으로 작동합니다.

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 

InputStream stream = getContentResolver().openInputStream(data.getData()); 
mBitmap = BitmapFactory.decodeStream(stream,null,options); 
stream.close(); 

을하지만 지금은 비트 맵은 NULL입니다! :

그래서,이 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html에 따라 코드를 변경

+0

http://stackoverflow.com/questions/6118464/android-out-of-memory-error-bitmap-is-too-big?rq=1 – Ridcully

+0

options.inJustDecodeBounds = 사실의 사용 가능한 복제; 이걸 사용하면 비트 맵의 ​​크기 만 디코드하고 배부하지 않으므로 null입니다. options.inScale을 사용하여 비트 맵의 ​​크기를 줄이십시오. –

+0

http://stackoverflow.com/a/4665992/1615280 – bjorncs

답변

1

응용 프로그램이 많은 고해상도 비트 맵 (비트 맵 메모리 파티션은 19677KB)을 사용하는 것처럼 보입니다. '힙'과 '할당 된'은 거의 정상이며 아무 문제가 없어야합니다. 메모리에서 사용하지 않는 비트 맵을 제거했는지 확인하십시오. bitmap.recycle()을 호출하거나 참조를 null로 설정하여 메모리에서 비트 맵을 해제 할 수 있습니다. 성능상의 이유로 비트 맵을 캐시하려면 LruCache을보십시오.

+0

[LruCache] (http://developer.android.com /training/displaying-bitmaps/cache-bitmap.html) 좀 더 자세한 내용이있는 예 – zapl

+0

19MB (아마) 인 내 갤러리 (htc 이미지가 설치되어 있음)에서 예제 이미지를 선택할 때이 오류가 발생합니다. – enfix

+0

좋아요, 오류 메시지에서 언급 한 985120 바이트가 이미지의 크기라고 생각했습니다. 19MB는 많은 양의 데이터입니다. 저비용 HTC 폰을 사용하는 경우에는 좋은 해결책이 없을 수 있습니다. 구문 분석하는 동안 즉석에서 이미지 크기를 조정하는 기능이 있습니다. – bjorncs

0

필자는 항상 inSampleSize를 증가시키고 OutOfMemoryError를 잡는 while 루프에서 디코딩을 래핑합니다. 이렇게하면 가능한 최대 해상도 이미지를 얻을 수 있습니다. 항상 LRU 캐시를 사용하십시오!

Bitmap image; 
    boolean success = false;int counter = 0; 
    while (success == false && counter < 10) 
    { 
     try 
     { 
      image = BitmapFactory.decodeFile(photoPath, options); 
      success = true; 
     } 
     catch(OutOfMemoryError e) 
     { 
      System.gc(); 
      options.inSampleSize++; 
      counter++; 
     } 
    } 
+0

작동하지 마십시오. 이미지는 항상 null입니다. – enfix