保存的图像比从图库中挑选的图像大
我使用我的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;
}
以及可以降低图像的质量 -
image.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);
这节省60%的质量图像。
保存解压缩的图像是加起来的空间。
其实您使用.PNG
文件格式来保存图像,但它的无损格式不能压缩图像的大小。我把你的代码修正一些,使用它,如果你发现图像大小有任何变化,我会恢复原状。
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, 70, 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 + ".JPEG");
} else {
return null;
}
currentphotopath = "file:" + file.getAbsolutePath();
return file;
}
这将图像的大小减少到了30%左右。但我想知道100是什么意思。这不是说原来的形象。 –
如果你想复制摄取的图像,然后打开一个输入流得到的URI和为您的文件路径的文件输出流。然后从循环块中的输入流中读取并将它们写入文件输出流。正常的副本。
InputStream is = getContentResolver().openInputStrea(data.getData());
FileOutputStream fos = new FileOutputStrea(path);
@deepakbajpay刚刚阅读这个答案,这是你应该遵循的唯一方法 – pskink
如果将“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%,而不是原来的图像。 –