2013-02-07 43 views
0

我想將圖像從我的SD卡保存到Android設備庫中。問題是,圖像是保存爲JPEG而不是PNG,並且它損失了質量,看起來非常糟糕。如何將SD卡中的圖像保存爲png的Android圖庫?

這是我的代碼:

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(); 

爲什麼圖像保存爲JPEG,我怎麼能保存它沒有鬆動的質量? b.t.w - SD卡上的圖像質量非常好,如果我將圖像添加到意圖並通過電子郵件發送 - 質量非常好。

回答

1

你可以使用這段代碼以便將圖像保存爲PNG格式。

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();