我使用我的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%,而不是原来的图像。 –