3

クライアントが画像を受信し、画像のデータを Bitmap クラスに格納するソケットを使用するアプリケーションを作成しました。

この Bitmap オブジェクトからmyimage.pngまたはmyimage.bmpという名前のファイルを作成する方法を教えてください。

String base64Code = dataInputStream.readUTF();
byte[] decodedString = null;
decodedString = Base64.decode(base64Code);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
4

2 に答える 2

5

画像をPNG形式で保存するには、次のコードを試してください

try {
   FileOutputStream out = new FileOutputStream(filename);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
   e.printStackTrace();
}
out.flush();
out.close();

ここで、100 は圧縮で保存する品質です。0 から 100 の間の任意のものを渡すことができます。桁を下げると、サイズが小さくなり品質が低下します。

ノート

Android マニフェスト ファイルで権限を取得する必要があります。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

編集

画像を.BMP形式で保存するには、Android Bitmap Utilが役立ちます。非常に単純な実装です。

String sdcardBmpPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sample_text.bmp";
Bitmap testBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_text);
AndroidBmpUtil bmpUtil = new AndroidBmpUtil();
boolean isSaveResult = bmpUtil.save(testBitmap, sdcardBmpPath);
于 2013-09-03T06:27:57.320 に答える
0
try {
   FileOutputStream out = new FileOutputStream(filename);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
   e.printStackTrace();
} finally {
   out.close();
}
于 2013-09-03T06:29:45.183 に答える