MediaStore.Images.Media.getBitmap(ContentResolver cr,Uri url)方法可以从一个已知的图片Uri中获得图片的bitmap对象,其中ContentResolver通常可以通过在Activity中调用的getContentResolver()方法中获取。Uri地址就是类似 content://media/external/images/media/2 的地址,也就是Content Provider定义的地址形式。但是这种获取bitmap的方式在图片较大的时候并不可取。为什么呢?
从getBitmap的参数可以看出,我们没有传递所要得到的bitmap大小信息,那么getBitmap的返回的bitmap究竟有多大我们就无从知晓,完全取决于getBitmap本身,最乐观的结果是getBitmap能智能的判断我们的需求,返回一个合适的bitmap,但是可能性很小。到底getBitmap中做了什么还是直接去看他的实现源码吧:
android.provider.MediaStore中找到Images.Media的getBitmap
方法:
1
2
3
4
5
6
7
|
public
static final Bitmap getBitmap(ContentResolver cr, Uri url) throws
FileNotFoundException, IOException { InputStream
input = cr.openInputStream(url); Bitmap
bitmap = BitmapFactory.decodeStream(input); input.close(); return
bitmap; } |
呵呵,以为是一个很复杂的方法,其实它很简单很粗暴,返回的是原始大小的bitmap。
MediaStore.Images.Media自身的getBitmap
不是很可靠。那我们来自定义一个getBitmap吧:
取名叫getThumbnail,他能返回一个指定大小的bitmap:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public
static Bitmap getThumbnail(Uri uri,int size) throws FileNotFoundException, IOException{ InputStream
input = this .getContentResolver().openInputStream(uri); BitmapFactory.Options
onlyBoundsOptions = new
BitmapFactory.Options(); onlyBoundsOptions.inJustDecodeBounds
= true ; onlyBoundsOptions.inDither= true ; //optional onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888; //optional BitmapFactory.decodeStream(input,
null ,
onlyBoundsOptions); input.close(); if
((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) return
null ; int
originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth; double
ratio = (originalSize > size) ? (originalSize / size) : 1.0; BitmapFactory.Options
bitmapOptions = new
BitmapFactory.Options(); bitmapOptions.inSampleSize
= getPowerOfTwoForSampleRatio(ratio); bitmapOptions.inDither= true ; //optional bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888; //optional input
= this .getContentResolver().openInputStream(uri); Bitmap
bitmap = BitmapFactory.decodeStream(input, null ,
bitmapOptions); input.close(); return
bitmap; } private
static int getPowerOfTwoForSampleRatio(double ratio){ int
k = Integer.highestOneBit((int)Math.floor(ratio)); if (k==0)
return
1; else
return
k; } |
其中参数size可以认为是宽和高的最大值。
转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0826/1665.html