2017-06-26 97 views
0

嗨,我試圖保存圖片下載設備存儲我有這種方法來保存圖片在存儲中,但保存後,我發現圖片質量不好,請幫我我想保存圖片與同一部開拓創新的質量保存後使用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); 
} 
+1

嘗試使用'.dontTransform()'。參考https://github.com/bumptech/glide/issues/1227 –

+0

@ wick.ed不工作:( – pic

+0

aww :('.into(new SimpleTarget (100,100)'這可能是問題嗎?增加它看看它是否有幫助,使它600,600或更多的測試,並嘗試改變'Bitmap.CompressFormat.JPEG'爲PNG。 –

回答

0

這條線:

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

字面上的意思是你想用100像素的寬度和高度100像素,這是真的小的圖像,我99.99%地肯定這是你的意思是什麼「質量差」。

如果你想要100%的原始圖像,你應該這樣做:

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

「目標」是內滑翔類,它具有「SIZE_ORIGINAL」不變。

這會給你完整的圖像,原來的質量,然後你可以保存。

0

你Bitmap.compress已經在最高質量的,您可以更改格式PNG,但you'll沒有壓縮的圖像,因爲PNG是一種無損格式。

您也可以更改圖片的尺寸,將SimpleTarget<Bitmap>(100,100)更改爲原始尺寸。