I have problem with size of picture when I’m doing conversion from PNG to JPEG, and then JPEG to PNG.
public void onClick(View v) {
String imageFileName = "/sdcard/Penguins2.png";
File imageFile = new File(imageFileName);
if (imageFile.exists()) {
// Load the image from file
myBitmap = BitmapFactory.decodeFile(imageFileName);
// Display the image in the image viewer
myImageView = (ImageView) findViewById(R.id.my_image_view);
if (myImageView != null) {
myImageView.setImageBitmap(myBitmap);
}
}
}
Conversion:
private void processImage() {
try {
String outputPath = "/sdcard/Penguins2.jpg";
int quality = 100;
FileOutputStream fileOutStr = new FileOutputStream(outputPath);
BufferedOutputStream bufOutStr = new BufferedOutputStream(
fileOutStr);
myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
bufOutStr.flush();
bufOutStr.close();
} catch (FileNotFoundException exception) {
Log.e("debug_log", exception.toString());
} catch (IOException exception) {
Log.e("debug_log", exception.toString());
}
myImageView.setImageBitmap(myBitmap);
After processing this operation I just change these lines:
String imageFileName = "/sdcard/Penguins2.png";
to
String imageFileName = "/sdcard/Penguins2.jpg";
and
String outputPath = "/sdcard/Penguins2.jpg";
(...)
myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
to
String outputPath = "/sdcard/Penguins2.png";
(...)
myBitmap.compress(CompressFormat.PNG, quality, bufOutStr);
Size of image changed from 585847 to 531409 (in DDMS)
I want to do such thing because I’d like to use PNG which is lossless for some image processing.
Then converse image to jpeg and send as MMS, I’m not sure but I think JPEG is only format which is support by all devices in MMS. Receiver would open image and converse it back to png without losing data.
In addition to the @Sherif elKhatib answer, if you check the documentation: http://developer.android.com/reference/android/graphics/Bitmap.html#compress%28android.graphics.Bitmap.CompressFormat,%20int,%20java.io.OutputStream%29
You can see that the PNG images don’t use the quality paremeter: