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);を使用して圧縮し、前と同じ操作を実行できます。

関連する問題