Desert Ice
Desert Ice

Reputation: 4600

Saving a Bitmap in Android

I am trying to implement a Steganography project for Android. I have manipulated the pixel values and created a new bitmap. Now when i store the bitmap into the phone memory or memory card using

//fo denotes File output Stream
Bitmap.compress(Bitmap.CompressFormat.JPEG,100,fo);
//OR
Bitmap.compress(Bitmap.CompressFormat.PNG,100,fo);   

and try accessing the pixels back using getPixels();

the values are reverted back to the original bitmap i.e. rather than the manipulated bitmap. Can anybody figure why this is?

Upvotes: 1

Views: 1296

Answers (2)

yoah
yoah

Reputation: 7230

JPEG is lossy, it can change pixel values when compressing. Use PNG if you want to preserve colors.

Upvotes: 1

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

File myDir=new File("/sdcard/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

look at this answer https://stackoverflow.com/a/7887114/964741

Upvotes: 1

Related Questions