1. 通过Exif获取图片缩略图:
通过path,我们从exif信息中获取缩略图:
public static Bitmap fromExif(final String filePath, final int width, final int height) {
if (filePath == null) {
return null;
}
ExifInterface exifIF;
try {
exifIF = new ExifInterface(filePath);
} catch (IOException e) {
exifIF = null;
}
byte[] source = null;
Matrix mat = null;
if (exifIF != null) {
source = ();
mat = getMatrixToRotate(exifIF);
}
if (source == null) {
return null;
}
Bitmap bitmap = (source, 0, );
if (bitmap == null) {
return null;
}
Bitmap ret = resizeForThumbnail(bitmap, width, height, mat);
return ret;
}
getMatrixToRotate是从exif信息中获取旋转角度并生成矩阵用来,因为我们从exif中拿到的缩略图可能旋转角度不对,因此需要从exif中获取大图的旋转角度,然后可以对缩略图旋转,getMatrixToRotate代码如下:
public static Matrix getMatrixToRotate(ExifInterface exifIf) {
if (exifIf == null) {
return null;
}
Matrix ret = new Matrix();
int orientation = (ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
(90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
(180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
(270);
break;
default:
ret = null;
break;
}
return ret;
}
拿到缩略图的byte数组后转化为bitmap,之后对bitmap进行缩放和旋转,这样就得到了我们最终想要的缩略图:
public static Bitmap resizeForThumbnail(Bitmap source, final int width, final int height,Matrix matrix) {
int w = ();
int h = ();
if (w > h) {
source = (source, (w - h) / 2, 0, h, h,matrix,false);
} else if (w < h) {
source = (source, 0, (h - w) / 2, w, w,matrix,false);
}
return source;
}
2. 从ContentUri获取缩略图
首先从content uri获取到图片的真实路径:
public static String getFilePathFromContentUri(Uri contentUri, ContentResolver contentResolver) {
String filePath;
String[] filePathColumn = {};
Cursor cursor = (contentUri, filePathColumn, null, null, null);
// 也可用下面的方法拿到cursor
// Cursor cursor = (contentUri, filePathColumn, null, null, null);
();
int columnIndex = (filePathColumn[0]);
filePath = (columnIndex);
();
return filePath;
}
获取到filepath之后就可以利用标题1,从Exif获取缩略图,
3. 通过大图缩放生成缩略图
并不是所有的图片的exif信息中都包含缩略图,如果我们通过exif获取到的缩略图为空,就只能通过大图缩放获取缩略图。
public static Bitmap fromFilePath(final Context context, final String filePath, final int width,
final int height) {
Bitmap bitmap = decodeScaled(filePath, width, height);
int degree = getExifOrientation(filePath);
Matrix matrix = new Matrix();
(degree);
Bitmap ret = resizeForThumbnail(bitmap, width, height, matrix);
if (bitmap != ret) {
();
}
return ret;
}
还是不能忘记旋转缩略图:
public static Bitmap decodeScaled(String filePath, int width, int height) {
opt = new ();
= true;
(filePath, opt);
int scaleW = / width;
int scaleH = / height;
= (scaleW, scaleH);
= false;
return (filePath, opt);
}
4. 获取视频缩略图:
public static Bitmap videoThumbnailFromPath(String path, int width, int height) {
Bitmap bmp = null;
try {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
(path);
bmp = (0, MediaMetadataRetriever.OPTION_CLOSEST_SYNC, 512, 384);
} catch (Exception e) {
();
}
if (bmp == null) {
return null;
}
Bitmap ret = resizeForThumbnail(bmp, width, height,null);
();
return ret;
}