缩略图是智能终端中常用的一个功能,在Android,多媒体文件(视频和图片)都是有缩略图的,在很多应用中,我们需要获取这些缩略图,下面是Android的一个实现:
代码:缩略图的实现
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卡中的文件可以通过数据库android.provider.MediaStore.Images.
Thumbnails来读取。
具体的缩略图可以通过getThumbnail()获取。在Android中,缩略图分为微型(MICRO_KIND)和迷你型(MINI_KIND)两种缩略模式。基于MediaStore的数据库的好处是提取缩略图时,不用自己计算。