80,351
社区成员
发帖
与我相关
我的任务
分享
private static final int VIDEO_WIDTH = 160;
private static final int VIDEO_HEIGHT = 200;
private Bitmap getImageThumbnail(String imagePath) {
System.out.println("getImageThumbnail - imagePath: " + imagePath);
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //不申请内存 计算图片比例
// 获取这个图片的宽和高,注意此处的bitmap为null
bitmap = BitmapFactory.decodeFile(imagePath, options);
options.inJustDecodeBounds = false; //设为 false 申请内存
// 计算缩放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / VIDEO_WIDTH;
int beHeight = h / VIDEO_HEIGHT;
int be = 4;
if (beWidth < beHeight && beHeight >= 1) {
be = beHeight;
}
if (beHeight< beWidth && beWidth >= 1) {
be = beWidth;
}
if (be <= 0) {
be = 1;
} else if (be > 3) {
be = 3;
}
options.inSampleSize = be;
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
options.inPurgeable = true;
options.inInputShareable = true;
try {
// 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(imagePath, options);
// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, VIDEO_WIDTH, VIDEO_HEIGHT,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
} catch (OutOfMemoryError e) {
System.gc();
bitmap = null;
}
return bitmap;
}
07-14 16:58:37.924: I/System.out(22725): imCRadapter - position: 1
07-14 16:58:37.924: I/System.out(22725): imCRadapter - imagePath: /storage/emulated/0/chinahotline/myImage/IMG_20150707_1131557.png
07-14 16:58:37.924: I/System.out(22725): imCRadapter - setImageResource: 1
07-14 16:58:37.924: I/System.out(22725): getImageThumbnail - imagePath: /storage/emulated/0/chinahotline/myImage/IMG_20150707_1131557.png
07-14 16:58:37.954: I/System.out(22725): imCRadapter - position: 0
07-14 16:58:37.954: I/System.out(22725): imCRadapter - imagePath: /storage/emulated/0/chinahotline/myImage/IMG_20150706_1225S97.png