保存后使用Glide库后图像质量不佳
问题描述:
嗨,我试图保存图片下载设备存储我有这种方法来保存图片在存储中,但保存后,我发现图片质量不好,请帮我我想保存图片与同一部开拓创新的质量保存后使用Glide库后图像质量不佳
Glide.with(mContext)
.load("YOUR_URL")
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
saveImage(resource);
}});
private String saveImage(Bitmap image) {
String savedImagePath = null;
String imageFileName = "JPEG_" + "FILE_NAME" + ".jpg";
File storageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/YOUR_FOLDER_NAME");
boolean success = true;
if (!storageDir.exists()) {
success = storageDir.mkdirs();
}
if (success) {
File imageFile = new File(storageDir, imageFileName);
savedImagePath = imageFile.getAbsolutePath();
try {
OutputStream fOut = new FileOutputStream(imageFile);
image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
// Add the image to the system gallery
galleryAddPic(savedImagePath);
Toast.makeText(mContext, "IMAGE SAVED"), Toast.LENGTH_LONG).show();
}
return savedImagePath;
}
private void galleryAddPic(String imagePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(imagePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
答
这条线:
.into(new SimpleTarget<Bitmap>(100,100)
字面上的意思是你想用100像素的宽度和高度100像素,这是真的小的图像,我99.99%地肯定这是你的意思是什么“质量差”。
如果你想要100%的原始图像,你应该这样做:
.into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
“目标”是内滑翔类,它具有“SIZE_ORIGINAL”不变。
这会给你完整的图像,原来的质量,然后你可以保存。
答
你Bitmap.compress已经在最高质量的,您可以更改格式PNG,但you'll没有压缩的图像,因为PNG是一种无损格式。
您也可以更改图片的尺寸,将SimpleTarget<Bitmap>(100,100)
更改为原始尺寸。
尝试使用'.dontTransform()'。参考https://github.com/bumptech/glide/issues/1227 –
@ wick.ed不工作:( – pic
aww :('.into(new SimpleTarget(100,100)'这可能是问题吗?增加它看看它是否有帮助,使它600,600或更多的测试,并尝试改变'Bitmap.CompressFormat.JPEG'为PNG。 –