2017-11-20 9 views
0

이미지를 데이터베이스로 변환하는 기능이 있습니다 (이미지를 데이터베이스 sqlite에 저장하기 위해). 이미지를 압축하고 메모리 오류를 피하는 방법에 문제가 있습니까? 이것은 내 코드입니다 미리 감사드립니다. 크기가 조정 된 비트 맵을 얻기 위해이 방법 Bitmap bitmap = GetAttachmentsUtils.getResizedBitmap(bitmap, maxSize);이 기능을이미지를 비트 맵으로 변환하고 압축하십시오.

public static Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 

     float bitmapRatio = (float)width/(float) height; 
     if (bitmapRatio > 1) { 
      width = maxSize; 
      height = (int) (width/bitmapRatio); 
     } else { 
      height = maxSize; 
      width = (int) (height * bitmapRatio); 
     } 
     return Bitmap.createScaledBitmap(image, width, height, true); 
    } 

그리고 전화 :

public byte[] ConverttoArrayByte(ImageView img) 
{ 
    try{ 
     BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable(); 
     Bitmap bitmap = bitmapDrawable.getBitmap(); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     return stream.toByteArray(); 
    }catch (NullPointerException e){ 
     Log.d("Tag", "Null"); 
     e.printStackTrace(); 
    } 
    return null; 
} 

답변

0

당신은 이미지 크기를 조정 작은 기능을 사용하려고 할 수 있습니다.

그런 다음 bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);을 사용하여 압축하고 이전에 수행했던 것과 동일한 작업을 수행 할 수 있습니다.