Skip to content

Instantly share code, notes, and snippets.

@NashLegend
Last active August 29, 2015 14:02
Show Gist options
  • Save NashLegend/d60cc5affcc1c681eb4e to your computer and use it in GitHub Desktop.
Save NashLegend/d60cc5affcc1c681eb4e to your computer and use it in GitHub Desktop.
音乐、视频、apk、图片缩略图提取
/**
* 提取文件缩略图,指定缩略图大小, 如果没有指定context,将不会取到apk文件的缩略图
*
* @param file
* @param width
* @param height
* @return
*/
public static Bitmap extractFileThumbnail(File file, int width, int height,
Context context) {
int type = getFileType(file);
Bitmap thumb = null;
switch (type) {
case FILE_TYPE_IMAGE:// 获取图像文件缩略图
thumb = getImageFileThumbnail(file, width, height);
break;
case FILE_TYPE_VIDEO:// 获取视频文件缩略图
thumb = getVideoFileThumbnail(file, width, height);
break;
case FILE_TYPE_AUDIO:// 获取音乐文件缩略图,
if (getFileSuffix(file).toLowerCase().equals("mp3")) {
Bitmap tmp = getMP3Thumbnail(file);
thumb = ThumbnailUtils.extractThumbnail(tmp, width, height);
}
break;
case FILE_TYPE_APK:// 获取apk文件按指定尺寸缩放过的缩略图,
thumb = getApkResizedIcon(context, file.getAbsolutePath(), width,
height);
break;
default:
break;
}
return thumb;
}
/**
* 获取图片文件的缩略图
*
* @param file
* @param width
* @param height
* @return
*/
public static Bitmap getImageFileThumbnail(File file, int width, int height) {
Bitmap bitmap = null;
String path = file.getAbsolutePath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 只获取这个图片的宽和高
bitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false;
// 计算缩放比,出现错误的时候有可能为-1
int h = options.outHeight;
int w = options.outWidth;
if (h > 0 && w > 0) {
int beWidth = w / width;
int beHeight = h / height;
int be = 1;
if (beWidth < beHeight) {
be = beWidth;
} else {
be = beHeight;
}
if (be <= 0) {
be = 1;
}
options.inSampleSize = be;
// 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(path, options);
// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}
public static Bitmap getVideoFileThumbnail(File file, int width, int height) {
// MINI_KIND: 512 x 384 ; MICRO_KIND: 96 x 96
Bitmap thumb = null;
if (width > 96 || height > 96) {
// 大于96 x 96,则取MINI_KIND的缩略图并在此基础上再次抽取
thumb = ThumbnailUtils.extractThumbnail(ThumbnailUtils
.createVideoThumbnail(file.getAbsolutePath(),
Thumbnails.MINI_KIND), width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
} else if (width == 96 && height == 96) {
thumb = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(),
Thumbnails.MICRO_KIND);
} else {
// 小于96 x 96,则取MICRO_KIND的缩略图并在此基础上再次抽取
thumb = ThumbnailUtils.extractThumbnail(ThumbnailUtils
.createVideoThumbnail(file.getAbsolutePath(),
Thumbnails.MICRO_KIND), width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return thumb;
}
/**
* http://blog.csdn.net/toni001/article/details/6724785
*
* @param file
* @return
*/
public static Bitmap getMP3Thumbnail(File file) {
int buffSize = 204800;//
FileInputStream mp3ips = null;
Bitmap bitmap = null;
try {
mp3ips = new FileInputStream(file);
if (buffSize > mp3ips.available()) {
buffSize = mp3ips.available();
}
byte[] buff = new byte[buffSize];
mp3ips.read(buff, 0, buffSize);
if (indexOf("ID3".getBytes(), buff, 1, 512) == -1) {
// No ID3V2
return null;
}
if (indexOf("APIC".getBytes(), buff, 1, 512) != -1) {
int searLen = indexOf(new byte[] {
(byte) 0xFF, (byte) 0xFB
},
buff);
int imgStart = indexOf(new byte[] {
(byte) 0xFF, (byte) 0xD8
},
buff);
int imgEnd = lastIndexOf(
new byte[] {
(byte) 0xFF, (byte) 0xD9
}, buff, 1,
searLen) + 2;
byte[] imgb = cutBytes(imgStart, imgEnd, buff);
bitmap = BitmapFactory.decodeByteArray(imgb, 0, imgb.length);
} else {
// No APIC
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (mp3ips != null) {
try {
mp3ips.close();
} catch (Exception e2) {
}
}
}
return bitmap;
}
/**
* 正向索引
*/
public static int indexOf(byte[] tag, byte[] src) {
return indexOf(tag, src, 1, src.length);
}
/**
* 获取第index个的位置<br />
* index从1开始
*/
private static int indexOf(byte[] tag, byte[] src, int index, int len) {
if (len > src.length) {
try {
throw new Exception("大于总个数");
} catch (Exception e) {
e.printStackTrace();
}
}
int tagLen = tag.length;
byte[] tmp = new byte[tagLen];
for (int j = 0; j < len - tagLen + 1; j++) {
for (int i = 0; i < tagLen; i++) {
tmp[i] = src[j + i];
}
// 判断是否相等
for (int i = 0; i < tagLen; i++) {
if (tmp[i] != tag[i])
break;
if (i == tagLen - 1) {
return j;
}
}
}
return -1;
}
/**
* 倒序获取第index个的位置<br />
* index从1开始
*/
private static int lastIndexOf(byte[] tag, byte[] src, int index, int len) {
if (len > src.length) {
try {
throw new Exception("大于总个数");
} catch (Exception e) {
e.printStackTrace();
}
}
int size = 0;
int tagLen = tag.length;
byte[] tmp = new byte[tagLen];
for (int j = len - tagLen; j >= 0; j--) {
for (int i = 0; i < tagLen; i++) {
tmp[i] = src[j + i];
}
for (int i = 0; i < tagLen; i++) {
if (tmp[i] != tag[i])
break;
if (i == tagLen - 1) {
size++;
return j;
}
}
}
return -1;
}
/**
* 截取byte[]
*/
private static byte[] cutBytes(int start, int end, byte[] src) {
if (end <= start || start < 0 || end > src.length) {
return null;
}
byte[] tmp = new byte[end - start];
for (int i = 0; i < end - start; i++) {
tmp[i] = src[start + i];
}
return tmp;
}
/**
* 获取apk文件按指定尺寸缩放过的缩略图
*
* @param context
* @param apkPath
* @return
*/
public static Bitmap getApkResizedIcon(Context context, String apkPath,
int width, int height) {
Bitmap thumb = getApkIcon(context, apkPath);
if (thumb != null) {
return ThumbnailUtils.extractThumbnail(thumb, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return thumb;
}
/**
* 获取apk文件缩略图将不会改变icon大小
*
* @param context
* @param apkPath
* @return
*/
public static Bitmap getApkIcon(Context context, String apkPath) {
Bitmap thumb = null;
PackageManager pm = context.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(apkPath,
PackageManager.GET_ACTIVITIES);
if (info != null) {
ApplicationInfo appInfo = info.applicationInfo;
appInfo.sourceDir = apkPath;
appInfo.publicSourceDir = apkPath;
try {
thumb = ((BitmapDrawable) appInfo.loadIcon(pm)).getBitmap();
} catch (OutOfMemoryError e) {
}
}
return thumb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment