2011.09.29(2)——— android 图片缩略图
参考:
http://www.eoeandroid.com/thread-101021-1-1.html
前面 我们说过视频缩略图的做法 今天 看一下图片的缩略图获得 直接上代码 一个工具类
package com.lp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
public final class BitmapUtil {
public static Bitmap decodeBitmap(String path, int displayWidth, int displayHeight) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
// op.inJustDecodeBounds = true;表示我们只读取Bitmap的宽高等信息,不读取像素。
Bitmap bmp = BitmapFactory.decodeFile(path, op); // 获取尺寸信息
// op.outWidth表示的是图像真实的宽度
// op.inSamplySize 表示的是缩小的比例
// op.inSamplySize = 4,表示缩小1/4的宽和高,1/16的像素,android认为设置为2是最快的。
// 获取比例大小
int wRatio = (int) Math.ceil(op.outWidth / (float) displayWidth);
int hRatio = (int) Math.ceil(op.outHeight / (float) displayHeight);
// 如果超出指定大小,则缩小相应的比例
if (wRatio > 1 && hRatio > 1) {
if (wRatio > hRatio) {
// 如果太宽,我们就缩小宽度到需要的大小,注意,高度就会变得更加的小。
op.inSampleSize = wRatio;
} else {
op.inSampleSize = hRatio;
}
}
op.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(path, op);
return Bitmap.createScaledBitmap(bmp, displayWidth, displayHeight, true);
// Bitmap.createScaledBitmap(Bitmap src,int detWidth,int detHeight,boolean filter)
// 从原Bitmap创建一个给定宽高的Bitmap
}
/**
* * 采用复杂计算来决定缩放 *
*
* @param path
* *
* @param maxImageSize
* *
* @return
*/
public static Bitmap decodeBitmap(String path, int maxImageSize) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, op);
// 获取尺寸信息
int scale = 1;
if (op.outWidth > maxImageSize || op.outHeight > maxImageSize) {
// Math.pow(double a, double b)表示返回第一个参数的第二个参数的次幂
// Math.log(double a)返回double的自然对数,底数是e
// Math.round(double e)返回最接近double的值
scale = (int) Math.pow(2, (int) Math.round(Math.log(maxImageSize / (double) Math.max(op.outWidth, op.outHeight)) / Math.log(0.5)));
}
op.inJustDecodeBounds = false;
op.inSampleSize = scale;
bmp = BitmapFactory.decodeFile(path, op);
return bmp;
}
public static Cursor queryThumbnails(Activity context) {
String[] columns = new String[] {
// Thumbnails表示的缩略图的意思
MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID };
return context.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, columns, null, null, MediaStore.Images.Thumbnails.DEFAULT_SORT_ORDER);
}
public static Cursor queryThumbnails(Activity context, String selection, String[] selectionArgs) {
String[] columns = new String[] { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID };
// public final Cursor managedQuery (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
// uri The URI of the content provider to query.
// projection List of columns to return.
// selection SQL WHERE clause.
// selectionArgs The arguments to selection, if any ?s are pesent
// sortOrder SQL ORDER BY clause.
return context.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, MediaStore.Images.Thumbnails.DEFAULT_SORT_ORDER);
}
// 通过缩略图的ID来查询
public static Bitmap queryThumbnailById(Activity context, int thumbId) {
String selection = MediaStore.Images.Thumbnails._ID + " = ?";
String[] selectionArgs = new String[] { thumbId + "" };
Cursor cursor = BitmapUtil.queryThumbnails(context, selection, selectionArgs);
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
cursor.close();
return BitmapUtil.decodeBitmap(path, 100, 100);
} else {
cursor.close();
return null;
}
}
public static Bitmap[] queryThumbnailsByIds(Activity context, Integer[] thumbIds) {
Bitmap[] bitmaps = new Bitmap[thumbIds.length];
for (int i = 0; i < bitmaps.length; i++) {
bitmaps[i] = BitmapUtil.queryThumbnailById(context, thumbIds[i]);
}
return bitmaps;
}
/**
* * 获取全部
*
* *
*
* @param context
* * @return
*
* */
public static List<Bitmap> queryThumbnailList(Activity context) {
List<Bitmap> bitmaps = new ArrayList<Bitmap>();
Cursor cursor = BitmapUtil.queryThumbnails(context);
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
Bitmap b = BitmapUtil.decodeBitmap(path, 100, 100);
bitmaps.add(b);
}
cursor.close();
return bitmaps;
}
public static List<Bitmap> queryThumbnailListByIds(Activity context, int[] thumbIds) {
List<Bitmap> bitmaps = new ArrayList<Bitmap>();
for (int i = 0; i < thumbIds.length; i++) {
Bitmap b = BitmapUtil.queryThumbnailById(context, thumbIds[i]);
bitmaps.add(b);
}
return bitmaps;
}
public static Cursor queryImages(Activity context) {
String[] columns = new String[] { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME };
return context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
}
public static Cursor queryImages(Activity context, String selection, String[] selectionArgs) {
String[] columns = new String[] { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME };
return context.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
}
public static Bitmap queryImageById(Activity context, int imageId) {
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = new String[] { imageId + "" };
Cursor cursor = BitmapUtil.queryImages(context, selection, selectionArgs);
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();
// return BitmapUtills.decodeBitmap(path, 260, 260);
return BitmapUtil.decodeBitmap(path, 220);
// 看看和上面这种方式的差别,看了,差不多
} else {
cursor.close();
return null;
}
}
/**
* * 根据缩略图的Id获取对应的大图 *
*
* @param context
* *
* @param thumbId
* *
* @return
* */
public static Bitmap queryImageByThumbnailId(Activity context, Integer thumbId) {
String selection = MediaStore.Images.Thumbnails._ID + " = ?";
String[] selectionArgs = new String[] { thumbId + "" };
Cursor cursor = BitmapUtil.queryThumbnails(context, selection, selectionArgs);
if (cursor.moveToFirst()) {
int imageId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
cursor.close();
return BitmapUtil.queryImageById(context, imageId);
} else {
cursor.close();
return null;
}
}
}
分享到:
相关推荐
基于因子分析的我国A股上市...争力评价——以医药企业为例_张澳.caj
2,jdk-170.tar.gz ——————————JDK1.7deb包 3,switch_java.sh -------------------------java其它版本切换 4,check_java.sh———————————版本核对 注意:使用命令接口切换时,需要将自己配置的...
python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目——Word助手.zip python项目...
微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+源码).zip 微信小程序——新闻客户端(截图+...
python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ读者书库.zip python项目——RCQ...
微信小程序——万年历(截图+源码).zip 微信小程序——万年历(截图+源码).zip 微信小程序——万年历(截图+源码).zip 微信小程序——万年历(截图+源码).zip 微信小程序——万年历(截图+源码).zip 微信小程序...
29. 爱——恨 30. 哭——笑 31. 这——那 32. 分——合 33. 圆——方 34. 远——近 35. 直——弯 36. 高——低 37. 真——假 38. 往——来 39. 新——旧 40. 卖——买 41. 外——内 42. 苦——甜 43. 活——死 44. ...
Android support.v7包
微信小程序——卡卡汽车 获取用户 设备信息(截图+源码).zip 微信小程序——卡卡汽车 获取用户 设备信息(截图+源码).zip 微信小程序——卡卡汽车 获取用户 设备信息(截图+源码).zip 微信小程序——卡卡汽车 ...
Android中文翻译组——Android中文API合集(4).chm
在Android开发中,实现图片浏览的全屏缩放效果是一项常见的需求,特别是在社交应用中,如QQ好友动态和微信朋友圈。这种功能不仅需要提供良好的用户体验,还需要考虑性能和内存优化,因为图片通常较大,处理不当可能...
应用层包过滤防火墙 一个简单的应用层包过滤防火墙实现 Qt/SQLite/NetFilter 编译过程可使用QtCreator编译,也可使用命令行 sudo qmake && make 来编译。 运行则执行 sudo ./firewall 即可。
基于IntraWeb的数据表格的多选实现 既可以单条操作,也可以多选操作。 delphi源代码。 BS开发Web网站开发,不需要安装服务器,Apache和IIS都不需要,自带企业级服务器。...————————————————
第4 章 Linux——App 后台应用最广泛的系统 .. 107 第5 章 Nginx——App 后台HTTP 服务的利器 . 126 第6 章 MySQL——App 后台最常用的数据库 .. 140 第7 章 Redis——App 后台高性能的缓存系统 . 160 第8 章 ...
OwlCarousel2-Thumbs, 启用 Owl Carousel 2.0的缩略图支持 carousel-2缩略图插件启用 Owl carousel 2.0的缩略图支持快速启动抓取最新版本并将它的slam默认owl插件。启用拇指$(document).ready(function(){ $('.owl-c
直流-直流变换器的matlab仿真模块,实现了直流降压的功能。
2. 门槛儿拉屎 ———— 里外都臭!这句歇后语用来形容坏名声四处传播。 3. 克拉玛依传捷报 ———— 出油(游)啦!这是一个新编歇后语,用谐音表达人们出游的场景。 4. 撅屁股看飞机 ———— 有眼无珠!这句歇后...
java毕业设计——百货中心供应链管理系统(论文+源代码+数据库+讲解视频).zip java毕业设计——百货中心供应链管理系统(论文+源代码+数据库+讲解视频).zip java毕业设计——百货中心供应链管理系统(论文+源代码+...
在Android应用开发中,APK文件是应用程序的打包格式,其中包含了所有必要的资源和代码。为了确保APK的安全性和完整性,Android系统使用签名机制。在APK中,MANIFEST.MF、CERT.SF和CERT.RSA这三个文件是签名过程的...
在本文中,我们将深入探讨如何基于Springmvc实现图片上传及生成缩略图的功能。Springmvc是Spring框架的一个重要模块,用于构建MVC模式的Web应用,它提供了强大的数据绑定、模型映射、视图渲染等功能,是Java开发中的...