2016-08-24 3 views
0

イメージをbase64エンコード形式で受け付けるサーバーにイメージをアップロードします。このコードを使用してビットマップをエンコードしますが、イメージの半分だけをエンコードし、サーバーはイメージ部分の半分しか受信しません。bitmapをbase64に変換するときのイメージを取得する

これは私のコードです。

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
      photo.compress(Bitmap.CompressFormat.PNG, 10, byteArrayOutputStream); 
      byte[] byteArray = byteArrayOutputStream .toByteArray(); 

      String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT); 
// Send to server(encoded) 

ギャラリーから画像をエンコードしようとすると、エラーがポップアップしてエラーが発生し、アプリケーションがクラッシュします。

java.lang.OutOfMemoryErrorを:私は、任意の助けをフル画像をエンコードしたいOOM

まで13777320の空きバイトと13メガバイトと15521174バイトの割り当てを割り当てることができませんでしたか?

+1

ストリームとして送信します。バイト配列に格納するのではなく、あなたの記憶が不足しているため。 – Doomsknight

+1

googleで検索 'OutOfMemoryError' –

+1

@KZoNE私の答えを試してください –

答えて

1

このコードを試してみてください。

BitmapFactory.Options options; 
options = new BitmapFactory.Options(); 

    // downsizing image as it throws OutOfMemory Exception for larger 
    // images 
    options.inSampleSize = 6; 
    Bitmap bitmap1 = BitmapFactory.decodeFile(fileUri1.getPath(), 
         options); 
       ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); 
       if(bitmap1 != null) { 
        bitmap1.compress(Bitmap.CompressFormat.PNG, 10, baos1); 
        byte[] b1 = baos1.toByteArray(); 
        bitmapstring1 = Base64.encodeToString(b1,  

        Base64.DEFAULT); 
+0

答えがありがとうございます。しかし、画像の品質が低下すると感じています。実際に私はそれを低品質にしたくありませんか?品質を落として送信するコードを知っていますか? 私は、bitmap1.compress(Bitmap.CompressFormat.PNG、100、baos1)を使う方が良いと思います。 あなたはどう思いますか? – KZoNE

+1

bitmap1.compress(Bitmap.CompressFormat。PNG、100、baos1);問題は発生しません@KZoNE –

+0

おかげで@Daminiの問題を解決しました – KZoNE

0
public String getStringImage(Bitmap bmp) { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] imageBytes = baos.toByteArray(); 
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT); 
    Log.d(TAG, "getStringImage: "+encodedImage.trim()); 
    return encodedImage; 
} 

あなたは非常に高解像度の画像を使用してメモリに直接ロードしている可能性があるため、この

+1

彼は彼が投稿したコードとはどのように違うのですか(圧縮率は別として) – Doomsknight

+0

私はコードを投稿する前にそれを見ていませんでした – UserSharma

+0

質問。 :)他の答えは異なっています – Doomsknight

1

は、あなたが「OutOfMemoryErrorが発生」を取得する必要がありますしてみてくださいしたがって、base64のStringは大きすぎます。 OutOfMemoryErrorを避けるために、常に画像のサブサンプリングされたバージョンをメモリにロードしてください。ドキュメントからTake a look at this. :サンプリングされたバージョンを復号そして

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) { 

    final int halfHeight = height/2; 
    final int halfWidth = width/2; 

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both 
    // height and width larger than the requested height and width. 
    while ((halfHeight/inSampleSize) >= reqHeight 
      && (halfWidth/inSampleSize) >= reqWidth) { 
     inSampleSize *= 2; 
    } 
} 

return inSampleSize; 
} 

For example, it’s not worth loading a 1024x768 pixel image into memory if it will eventually be displayed in a 128x96 pixel thumbnail in an ImageView.

まず画像のためのサンプルサイズを計算し、ビットマップのサンプリングされたバージョンの後

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

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

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

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 
return BitmapFactory.decodeResource(res, resId, options); 
} 

ありますその後、ビットマップをbase64に変換します。

+0

私はすでにあなたのサンプルを利用していますが、まだ白い半分の画像があります。なぜそれが起こったのか知っていますか? –

関連する問題