2017-05-26 75 views
-1

我使用我的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

如果將「Bitmap.CompressFormat.JPEG,100」更改爲「Bitmap.CompressFormat.JPEG,10」會發生什麼?尺寸較小,質量最差。在你的情況下,這個比例似乎是1:3(1.4MB到4.2 MB)。因此,'Bitmap.CompressFormat.JPEG,33'應該做到這一點。另外請注意,JPEG使用LOSSY壓縮。因此,每次你保存一張圖片時,它的質量會變得更糟,更加惡劣。 –

+0

是的,但是100意味着保存的圖像質量應該是100%,即與圖像的尺寸相同。但是這裏圖像尺寸比原來增加。如果我把33而不是100,那麼會發生質量損失。 @Rotwang –

+0

這是完全錯誤的**。圖像在內存中解壓縮。因此,您正在保存解壓縮圖像的100%,而不是原來的圖像。 –

回答

0

以及可以降低圖像的質量 -

image.compress(Bitmap.CompressFormat.JPEG, 60, fileOutputStream); 

這節省60%的質量圖像。

保存解壓縮的圖像是加起來的空間。

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; 
} 
+0

這將圖像的大小減少到了30%左右。但我想知道100是什麼意思。這不是說原來的形象。 –

1

如果你想複製攝取的圖像,然後打開一個輸入流得到的URI和爲您的文件路徑的文件輸出流。然後從循環塊中的輸入流中讀取並將它們寫入文件輸出流。正常的副本。

InputStream is = getContentResolver().openInputStrea(data.getData()); 

FileOutputStream fos = new FileOutputStrea(path); 
+0

@deepakbajpay剛剛閱讀這個答案,這是你應該遵循的唯一方法 – pskink