[英]Image quality is bad after saving using Glide Library
您好,我正在尝试将下载的图片保存在设备存储中,我有此方法可以将图片保存在存储中,但是保存后我发现图片质量很差,请帮助我,我想以相同的原始质量保存图片
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);
}
您的Bitmap.compress已经达到最高质量,您可以将格式更改为PNG,但是由于PNG是无损格式,因此您将无法进行图像压缩。
您也可以更改图像的暗度,将SimpleTarget<Bitmap>(100,100)
更改为原始图像。
这行:
.into(new SimpleTarget<Bitmap>(100,100)
从字面上看,这意味着您想要的图像的宽度为100px,高度为100px,这确实非常小,我有99.99%的把握,这就是您所说的“质量差”的意思。
如果您想要100%原始图像,则应使用以下代码:
.into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
“目标”是Glide中的一个类,它具有“ SIZE_ORIGINAL”常量。
这样可以为您提供原始质量的完整图像,然后可以保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.