2013-08-01 2 views
2

메신저.장치에 비트 맵을 만드는 동안 메모리가 부족합니다. 고해상도 이미지에 문제가있는

1280x720 이미지의 경우 nodpi-drawable 폴더를 사용하고이 코드를 사용하여 크기를 조정하십시오.

public static Drawable scaleDrawable(Drawable d, int width, Activity cxt) 
    { 
     BitmapDrawable bd = (BitmapDrawable)d; 

     double oldWidth = bd.getBitmap().getWidth(); 
     double scaleFactor = width/oldWidth; 

     int newHeight = (int) (d.getIntrinsicHeight() * scaleFactor); 
     int newWidth = (int) (oldWidth * scaleFactor); 

     Drawable drawable = new BitmapDrawable(cxt.getResources(),MainScreen.getResizedBitmap(bd.getBitmap(),newHeight,newWidth)); 

     BitmapDrawable bd2 = (BitmapDrawable)drawable; 

     return drawable; 
    } 

    public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

     int width = bm.getWidth(); 
     int height = bm.getHeight(); 

     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     // create a matrix for the manipulation 
     Matrix matrix = new Matrix(); 

     // resize the bit map 
     matrix.postScale(scaleWidth, scaleHeight); 

     // recreate the new Bitmap 
     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

     return resizedBitmap; 
     } 

내가 화면이 320 × 480 인 경우 이미지가 320로 확장 및 비율을 유지할 수 있도록 witdh 화면에 이미지를 확장하는 코드를 사용, 난 이미지가 바닥에서 화면 밖으로 이동하면 걱정 말아.

잘 작동하지만 xhdpi 장치에서 정확히 720x1280의 화면이있는 삼성 Galaxy Note 2를 사용하려고 할 때 유용합니다. 라인에서 메모리 부족 예외와

그것은 충돌 :

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

내가 이미지가 720에서 720로 확장해야하지만 내 코드를 최적화하거나 뭔가 정말 나쁜해야하는 이유, 이해하지 못할.

나는 havent 1080x1920 장치에 시도했지만 너무 추락 것 같습니다.

누군가 코드를 보면 뭔가 나빠질 수 있습니다.

답변

7

사용이 방법은 ..이 Defination-

public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
    int reqHeight) { 

final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(path, options); 

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

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
Bitmap bmp = BitmapFactory.decodeFile(path, options); 
return bmp; 
} 
} 
    public int calculateInSampleSize(BitmapFactory.Options options, 
    int reqWidth, int reqHeight) { 

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; 
} 

에게 bitmap-

Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight); 

사용 크기를 조정하려면

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
    int reqWidth, int reqHeight) { 

.... 
..... 
return BitmapFactory.decodeResource(res, resId, options); 
} 
+0

내 이미지는 drawable-nodpi에 저장된 드로어 블이며, 그 방법은 내게 맞는 경로에서 decodeFile을 사용합니다. ? 그리고 어떻게? – Nanoc

+0

BitmapFactory.decodeResource (resources res, int id)를 사용하면 폴더에서 드로어 블에 액세스 할 수 있습니다. – Horschtele

0

이미지 처리 작업에는 유틸리티 클래스 BitmapFactory를 사용해야합니다. 또한 BitmapFactory.Options를 사용하여 비트 맵의 ​​입력/출력 크기를 조정하십시오. 비트 맵이 더 이상 필요하지 않으면 관련된 메모리를 해제해야합니다. 당신이 다음 BitmapFactory의 decodeResource 방법과 방법을 대체 할 자원을 사용하는 경우

관련 문제