I am using an activity to display a Full image and user has a button to save it.
Image is being loaded in imageview from URL where I have hosted image on internet.
I am saving an image as PNG file.
After saving image its quality is reduced. I know PNG is not lossless, but is there any way I can save an image as it is without any compromise with quality or resolution??
Here is the code:
buttonSave.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
fullSizeImage.buildDrawingCache();
Bitmap bm=fullSizeImage.getDrawingCache();
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + File.separator + "MyWallpapers");
if(!myDir.exists())
{
myDir.mkdir();
}
String fname = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())+".png";
File file = new File (myDir, fname);
/*if (file.exists ()) file.delete (); */
try
{
//FileOutputStream out = new FileOutputStream(file);
/*FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);*/
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, bytes);
file.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
bytes.flush();
bytes.close();
/*out.flush();
out.close();*/
Toast.makeText(FullSizeImageDisplay.this, "Wallpaper Saved in SDcard/MyWallpapers folder",Toast.LENGTH_SHORT).show();
String[] paths = {root + File.separator + "MyWallpapers"};
String[] mediaType = {"image/png"};
MediaScannerConnection.scanFile(FullSizeImageDisplay.this, paths, mediaType, null);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
catch (Exception e)
{
e.printStackTrace();
}
OutputStream fOut = null;
Uri outputFileUri;
}
});
}
Where fullSizeImage is may be an Image View.
I see problem is arising while you load the image. You must have used something like BitmapFactory.Options
Make following changes