Joshua Redfield
Joshua Redfield

Reputation: 2247

android better save image option?

Currently I am using the compress method to save an image taken with the camera hardware on the android phone to the SD card.

try {
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 10;
Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length,options);

fileOutputStream = new FileOutputStream(
sdImageMainDirectory.toString() +"/"+fileName+".png");

BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream); 
myImage.compress(CompressFormat.PNG, 100, bos);
bos.flush();
bos.close();

Now this works perfectly fine, however, the quality of image it saves hardly makes it worth taking the picture in the first place. I'm looking for a better way to save the picture at a higher quality.

Upvotes: 2

Views: 290

Answers (1)

Blackbelt
Blackbelt

Reputation: 157447

options.inSampleSize = 10;

here is your loss of quality: This will create an image of 1/10 in heigth and width

From the doc:

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.

Upvotes: 2

Related Questions