我使用我的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;
}
答案 0 :(得分:1)
实际上您使用.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;
}
答案 1 :(得分:1)
如果要复制拾取的图像,请打开获取的uri的输入流和文件路径的文件输出流。然后在循环chuncks中从输入流中读取并将它们写入文件输出流。正常副本。
InputStream is = getContentResolver().openInputStrea(data.getData());
FileOutputStream fos = new FileOutputStrea(path);
答案 2 :(得分:0)
你可以降低图像的质量 -
image.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream);
以60%的质量保存您的图像。
保存解压缩的图像会占用空间。