简体   繁体   中英

How do you save an image from an SD-card to android gallery as png?

I want to save image from my SD Card to the device gallery in android. The problem is that the image is savef as jpeg and not as png, and it loses quality and looks very bad.

this is my code:

File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "fileName.png");
Bitmap top = BitmapFactory.decodeFile(file.getPath());
MediaStore.Images.Media.insertImage(getContentResolver(), top, "someText" , "someDescription");
Toast toast = Toast.makeText(getBaseContext(), "Image saved to gallery", Toast.LENGTH_SHORT);
toast.show();

Why is the image saved as jpeg, and how can I save it without loosing the quality? btw - the image on the SD card is in very good quality, and if i'm adding the image to intent and sending it via email - the quality i very good.

you can use this code in order to save the image in png format.

Bitmap bitmap = createYourBitmap();
OutputStream stream = new FileOutputStream("/sdcard/test.png");
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
bitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM