您的位置:首页 > 移动开发 > Android开发

【Android-图片小技巧】通过给定的uri获取图片

2014-05-21 16:26 225 查看
/**
* Loads a bitmap that has been downsampled using sampleSize from a given url.
*/
public static Bitmap loadDownsampledBitmap(Context context, Uri uri, int sampleSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
options.inSampleSize = sampleSize;
return loadBitmap(context, uri, options);
}

/**
* Returns the bitmap from the given uri loaded using the given options.
* Returns null on failure.
*/
public static Bitmap loadBitmap(Context context, Uri uri, BitmapFactory.Options o) {
if (uri == null || context == null) {
throw new IllegalArgumentException("bad argument to loadBitmap");
}
InputStream is = null;
try {
is = context.getContentResolver().openInputStream(uri);
return BitmapFactory.decodeStream(is, null, o);
} catch (FileNotFoundException e) {
Log.e(LOGTAG, "FileNotFoundException for " + uri, e);
} finally {
Utils.closeSilently(is);
}
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android uri bitmap