2012-09-01 44 views
0

我的應用程序有一個按鈕,它允許用戶從圖庫中選擇一張照片並將其顯示在ImageView中。問題是,如你所知,從相機拍攝的圖像是巨大的,所以我想調整它們。找到ID來調整圖像大小(避免內存泄漏)

我發現這個方法,我想嘗試:

int inSample = 4; 

opts = new BitmapFactory.Options(); 
opts.inSampleSize = inSample; 

Bitmap b = BitmapFactory.decodeResource(c.getResources(), imgResId, opts); 

的問題是,我只有圖像的URI採取,我不知道如何將它轉換或找到它ID。

回答

1

Uris - 如果ContentProvider支持 - 可用於打開InputStream,然後可用於解碼Bitmap

所以它應該更像這樣。

int inSample = 4; 

opts = new BitmapFactory.Options(); 
opts.inSampleSize = inSample; 
ContentResolver cr = getContentResolver(); 
Uri uri = getUri(); // whatever it is 

// this will need some exception handling 
Bitmap b = BitmapFactory.decodeStream(cr.openInputStream(uri), opts); 
+0

方法的OpenStream(URI)是未定義該類型內容解析器 – Aldridge1991

+0

糟糕,是['ContentResolver的#openInputStream(URI)'](http://developer.android.com/reference/android/content /ContentResolver.html#openInputStream%28android.net.Uri%29) – zapl