2014-07-24 256 views
-1

我正在開發一個應用程序,其中,有一個配置文件頁面,用戶可以通過相機或圖庫設置其圖像。在android中縮小圖像大小programetically

現在,當用戶設置大圖像,然後我的應用程序停止不幸,我發現一個原因是,我們無法在圖像上設置圖像視圖超過一定的大小。

所以我決定在設置圖像視圖之前減小圖像大小。

在這裏,我發現了很多解決方案,但在所有它也減少了它的寬度和高度,但我不想減少它的寬度和高度。

我只想減少其百分比的大小。 E.x從1 mb到100 kb。

所以請幫助我如何在android中實現這一點。

+1

檢查這個環節,你可能需要改變圖像格式,減少百分比http://stackoverflow.com/questions/23240432/take-and-save-picture-with-specified-size-大小in-kb-s/23240768#23240768 – Saqib

+0

[將內存加載到Bitmap對象時出現內存不足的問題](http://support.microsoft.com/kb/477572/strange-out-of-memory-issue -while-loading-an-image-to-a-bitmap-object) –

回答

0

試試這種方式,希望這會幫助你解決你的問題。

public Bitmap decodeFile(String path) { 
    try { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, o); 
     // The new size we want to scale to 
     final int REQUIRED_SIZE = 70; 
     // Find the correct scale value. It should be the power of 2. 
     int scale = 1; 
     while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
     scale *= 2; 
     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeFile(path, o2); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
return null; 

} 
+0

我檢查了這個,但是在這裏我們需要傳遞大小「final int REQUIRED_SIZE = 70;」我不想要的,我想減少其百分比的大小 –