creating .bmp image file from Bitmap class
Asked Answered
P

2

3

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

Can anyone please 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);
Palazzo answered 3/9, 2013 at 6:20 Comment(0)
E
5

Try following code to save image as PNG format

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 quality to save in Compression. You can pass anything between 0 to 100. Lower the digit, poor quality with decreased size.

Note

You need to take permission in Android Manifest file.

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

Edit

To save your image to .BMP format, Android Bitmap Util will help you. It has very simple implementation.

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);
Essence answered 3/9, 2013 at 6:27 Comment(13)
when i tried this -------------FileOutputStream out = new FileOutputStream("Image.png"); bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);-----------------------i'm getting --------------java.io.FileNotFoundException: /Image.png: open failed: EROFS (Read-only file system)Palazzo
@user2318483, you can use like following. String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "your_file.png";Essence
sir when i send large image files such as 50KB 400X400 i'm getting java.nio.BufferOverflowExceptionPalazzo
@user2318483, are you doing your testing in Emulator?Essence
@user2318483, i tried with 1600 * 1200 resolution with size 1.3 MB image. I tested in Actual device and not getting such exception. I think it is due to low memory of emulator if you are running in it.. :)Essence
sir i'm not running the android in emulator.....but runs in android tablet connected on my PCPalazzo
@user2318483, okay then try BufferedOutputStream bos = new BufferedOutputStream(out); and bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); code.Essence
sir here's the place where i'm getting java.nio.BufferOverflowException--------------------------- boolean isSaveResult = bmpUtil.save(bitmap, sdcardBmpPath);Palazzo
@user2318483, oh dear.. bmpUtil.. i didn't implemented it with large sized. Code is not developed by me my friend. Sorry. In this case I can't help you.. :(Essence
sir where should i paste this code.........................BufferedOutputStream bos = new BufferedOutputStream(out); and bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);Palazzo
it was for my code my friend. The link which i provided is a library developed by other person. I didn't examine it. :(Essence
the problem is with the size sir......when i tried for small size it work perfectlyPalazzo
I examine the code.. there is array int[] pixels = new int[width * height]; which consume memory and other one is file operation. Is it possible to reduce size of your bitmap? Then try my answer at https://mcmap.net/q/1175821/-get-the-image-as-thumbnail/…Essence
T
0
try {
   FileOutputStream out = new FileOutputStream(filename);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
   e.printStackTrace();
} finally {
   out.close();
}
Tallu answered 3/9, 2013 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.