2013-03-06 115 views
0

我在用於測試的設備的SD卡上有一個Foto。 路徑:如何僅將一個圖像的縮略圖放入ImageView中?

Uri selectedImageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/test/test/test1.jpg")); 

(是的,媒體鏈接文件上exsits Micro-SD卡)

我已經成功地得到Micro-SD卡的所有圖像的縮略圖,但我只想要這個特定的一。

如何存檔?

回答

3

這裏是創建縮略圖代碼:

public static Bitmap decodeSampledBitmapFromFile(String path, 
    int reqWidth, int reqHeight) { // BEST QUALITY MATCH 

// First decode with inJustDecodeBounds=true to check dimensions 
final BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(path, options); 

// Calculate inSampleSize 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    options.inPreferredConfig = Bitmap.Config.RGB_565; 
    int inSampleSize = 1; 

    if (height > reqHeight) { 
     inSampleSize = Math.round((float)height/(float)reqHeight); 
    } 

    int expectedWidth = width/inSampleSize; 

    if (expectedWidth > reqWidth) { 
     //if(Math.round((float)width/(float)reqWidth) > inSampleSize) // If bigger SampSize.. 
     inSampleSize = Math.round((float)width/(float)reqWidth); 
    } 


options.inSampleSize = inSampleSize; 

// Decode bitmap with inSampleSize set 
options.inJustDecodeBounds = false; 

return BitmapFactory.decodeFile(path, options); 
} 
+0

謝謝,工作完美! – user2043332 2013-03-06 15:57:28

+0

歡迎您,@ user2043332。有一個很好的編碼:) – 2013-03-06 16:02:29