縮略圖是智能終端中常用的一個(gè)功能,在Android,多媒體文件(視頻和圖片)都是有縮略圖的,在很多應(yīng)用中,我們需要獲取這些縮略圖,下面是Android的一個(gè)實(shí)現(xiàn):
代碼:縮略圖的實(shí)現(xiàn)
private Bitmap createThumbnailBitmap(int thumbnailBoundsLimit, Uri uri)
{
int outWidth=mWidth;
int outHeight=mHeight;
int s=1;
while ((outWidth / s > thumbnailBoundsLimit)|| (outHeight / s > thumbnailBoundsLimit))
{
s *=2;
}
if (Log.isLoggable(LogTag.APP, Log.VERBOSE))
{
Log.v(TAG, "createThumbnailBitmap: scale="+s+", w="+outWidth/s+",h="+outHeight/s);
}
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=s;
InputStream input=null;
try {
input=mContext.getContentResolver().openInputStream(uri); //打開輸入流
return BitmapFactory.decodeStream(input, null, options); //圖像解碼
}
catch (FileNotFoundException e)
{
Log.e(TAG, e.getMessage(), e);
return null;
}
catch (OutOfMemoryError ex)
{
MessageUtils.writeHprofDataToFile();
throw ex;
}
finally {
if (input !=null)
{
try {
input.close();
}
catch (IOException e)
{
Log.e(TAG, e.getMessage(), e);
}
}
}
}
在Android中,對于SD卡中的文件可以通過數(shù)據(jù)庫android.provider.MediaStore.Images.
Thumbnails來讀取。
具體的縮略圖可以通過getThumbnail()獲取。在Android中,縮略圖分為微型(MICRO_KIND)和迷你型(MINI_KIND)兩種縮略模式;贛ediaStore的數(shù)據(jù)庫的好處是提取縮略圖時(shí),不用自己計(jì)算。