Creating a .bmp Image File from the Bitmap Class

I created an application that uses sockets in which the client receives the image and stores the image data in the Bitmap class ....

Can someone tell me how to create a file named myimage.png or myimage.bmp from this Bitmap object

String base64Code = dataInputStream.readUTF();
byte[] decodedString = null;
decodedString = Base64.decode(base64Code);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
+3
source share
2 answers

Try using the following code to save the PNG image

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

Here 100 is the quality to keep in compression. You can pass anything between 0 and 100. Lower the number, low quality with reduced size.

Note

Android.

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

Edit

.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);
+5
try {
   FileOutputStream out = new FileOutputStream(filename);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
   e.printStackTrace();
} finally {
   out.close();
}
0

Source: https://habr.com/ru/post/1535341/


All Articles