2012-11-27 3 views
0

활동이 전체 화면으로 이미지보기를 보여주는 애플리케이션을 구현 중입니다. 이제 응용 프로그램의 파일 디렉토리에 다운로드 한 일부 이미지가 있습니다. 이미지를 이미지로 설정하여 해당 활동에서 큰 이미지를 축소하려고합니다. 내가 응용 프로그램을 사용하여 10 ~ 15 분 후 OutOfMemory 오류가 나타납니다. 오류는 BitmapFactory의 decodeFile에 있습니다. 여기 내 코드입니다 : 지금 내가 포기하고android의 이미지 축소

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 decodeSampledBitmapFromFile(String imagePath, 
     int reqWidth, int reqHeight) { 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(imagePath, options); 

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

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(imagePath, options); 

} 

reqHeight = 500 reqWidth = 500

이 코드는 안드로이드 지침 페이지에 제시되어있다. 내가 빠져 나간 것이 있다면, 나를 도와주세요. 나는 많은 것을 시도했지만 어떤 해결책도 얻지 못했습니다.

답변

2

변경하려면 int inSampleSize = 1; inSampleSize = 2 또는 3 int로 변경하고 응용 프로그램을 다시 테스트하면 문제가 해결되기를 바랍니다. 나는 또한이 기술의 insted 제안, 거기에 모든 비트 맵 및 outOfMemoryError 될 것입니다 의해 dowanload 이미지를 모두 게으른로드 시도하십시오. 요는 게으른 로딩을 가질 수 있습니다 - https://github.com/thest1/LazyList

0

decodeSampledBitmapFromFile에 의해 반환 된 비트 맵 목록을 어딘가에 저장하는 경우 앱에 메모리 누수가 발생할 수 있습니다. 메모리 누수가 있는지 확인하고 Bitmap.recycle()을 사용하여 비트 맵을 재활용하고 안드로이드 여유 공간을 확보하십시오. 그것은 당신을 도울 것이

Bitmap resizedbitmap2 = Bitmap.createScaledBitmap(your_bitmap, width, height, 
         true); 

를 사용