2014-09-17 3 views
0

를로드하기위한 inSampleSize를의 최소값은 내가 큰 비트 맵을 다운 샘플링에 대한 inSampleSize의 최소 값을 얻기 위해 다음 코드를 작성 :는 SO 검색과 답변을 많이 읽고 나면 큰 비트 맵

public Bitmap load(Context context, String image_url) throws Exception { 
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

    bitmapOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(image_url, bitmapOptions); 

    final float imageSize = (float) bitmapOptions.outWidth * (float) bitmapOptions.outHeight * 4.0f/1024.0f/1024.0f; // MB 

    bitmapOptions.inSampleSize = (int) Math.pow(2, Math.floor(imageSize/MemoryManagement.free(context))); 
    bitmapOptions.inJustDecodeBounds = false; 

    return BitmapProcessing.modifyOrientation(BitmapFactory.decodeFile(image_url, bitmapOptions), image_url); 
} 

그리고

public class MemoryManagement { 

    public static float free(Context context) { 
     ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
     int memoryClass = am.getMemoryClass() - 10; 
     if (memoryClass < 1) memoryClass = 1; 
     return (float) memoryClass; 
    } 

} 

목표는 OutOfMemory 예외없이 비트 맵 샘플의 최대 크기를 얻는 것입니다. 이 코드를 신뢰할 수 있을까요?

+0

당신이 할 일을 배우려면 나에게 설명해 줄 수 있습니까? – mmlooloo

+0

@mmlooloo 비트 맵을로드하고 일부 효과를 적용해야합니다. 그래서 나는 OOM 예외없이 비트 맵 샘플의 최대 차원을 얻기 위해'inSampleSize'의 최소값을 찾으려고했습니다. –

답변

1

는 좀도 24

뭔가

public class MemoryManagement { 

    public static float free(Context context) { 
     ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
     int memoryClass = am.getMemoryClass() - 16; 
     if (memoryClass < 1) memoryClass = 1; 
     return (float) memoryClass; 
    } 

} 
0

추천 제한 자바 힙이 힙 크기가 16메가바이트로 제한됩니다 경우 장치의 16 대부분에 힙 테스트를 업그레이드 할 수 있다고 생각 Nadir 반환 값은 1MB입니다. 즉, 예를 들어 20MB 그림을 1MB 공간으로 확장해야하지만, 예를 들어 10MB를 사용할 수 있으므로 솔루션의 동적 크기 조정은 힙 크기에 관계없이 항상 16MB 또는 24MB 등의 이미지 공간을 사용합니다. :

public class MemoryManagement { 

    public static float free(Context context) { 
     ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
     int memoryClass = am.getMemoryClass(); 
     memoryClass = ((memoryClass *3)/5); // use 0.6 of your memory 
     if (memoryClass < 1) memoryClass = 1; 
     return (float) memoryClass; 
     } 
    }