我使用我的android應用程序從圖庫中選取了圖像並保存在Environment目錄中,而未對其進行任何處理。原始圖像大小爲1.4 Mb,但新保存的圖像大小超過4 Mb。 我使用的代碼如下。保存的圖像比從圖庫中挑選的圖像大
case REQUEST_CODE_FROM_GALLERY:
String datastring1=data.getDataString();
Uri uri = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
Log.e("..............",bitmap.getHeight()+","+bitmap.getWidth());
} catch (IOException e) {
e.printStackTrace();
}
storeImage(bitmap);
break;
private boolean storeImage(Bitmap image) {
File pictureFile = createDir(MEDIA_TYPE_IMAGE);
try {
FileOutputStream fileOutputStream = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private File createDir(int type) {
File file;
File filedir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "EffitientImageLoading");
if (!filedir.exists()) {
if (!filedir.mkdir())
Toast.makeText(this, "Directory could not be created!! Try Again"
, Toast.LENGTH_LONG).show();
}
String timestamp = new SimpleDateFormat("ddmmyy_hhmmss").format(new Date());
if (type == MEDIA_TYPE_IMAGE) {
file = new File(filedir.getPath() + File.separator +
"IMG_" + timestamp + ".png");
} else {
return null;
}
currentphotopath = "file:" + file.getAbsolutePath();
return file;
}
如果將「Bitmap.CompressFormat.JPEG,100」更改爲「Bitmap.CompressFormat.JPEG,10」會發生什麼?尺寸較小,質量最差。在你的情況下,這個比例似乎是1:3(1.4MB到4.2 MB)。因此,'Bitmap.CompressFormat.JPEG,33'應該做到這一點。另外請注意,JPEG使用LOSSY壓縮。因此,每次你保存一張圖片時,它的質量會變得更糟,更加惡劣。 –
是的,但是100意味着保存的圖像質量應該是100%,即與圖像的尺寸相同。但是這裏圖像尺寸比原來增加。如果我把33而不是100,那麼會發生質量損失。 @Rotwang –
這是完全錯誤的**。圖像在內存中解壓縮。因此,您正在保存解壓縮圖像的100%,而不是原來的圖像。 –