使用Glide Library保存后图像质量很差

时间:2017-06-26 17:24:50

标签: android save android-glide image-quality

嗨我想保存下载到设备存储中的图片我有这种方法可以在存储中保存图片但保存后我发现图片质量不好请帮帮我,我想保存具有相同原始质量的图片< / p>

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);
}

2 个答案:

答案 0 :(得分:0)

你的Bitmap.compress已经达到了最高质量,你可以将格式更改为PNG,但是你没有压缩图像,因为PNG是一种无损格式。

您也可以更改图片的尺寸,将SimpleTarget<Bitmap>(100,100)更改为原始尺寸。

答案 1 :(得分:0)

这一行:

.into(new SimpleTarget<Bitmap>(100,100)

字面意思是你想要的图像宽度为100px,高度为100px,这真的很小,我99.99%肯定这就是你对“质量差”的意思。

如果你想要100%的原始图像,你应该使用它:

.into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)

“目标”是Glide中的一个类,它具有“SIZE_ORIGINAL”常量。

这将为您提供原始质量的完整图像,然后您可以保存。

相关问题