2012-09-01 46 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