Android 获取文件的缩略图
fileitem 是个工具类保存文件的一些属性如名字,地址等
new AsyncLoadVideo().execute(fileItem); //视频其他类似
//视频
class AsyncLoadVideo extends AsyncTask
@Override
protected Object doInBackground(FileItem... params) {
String path = MediaContainerApplication.CACHE_PATH;
Bitmap bitmap;
Bitmap newBitmap;
FileItem item = params[0];
File thumbFile = new File(path + item.getFileName().replace(".", ""));
if (thumbFile.exists()) {
newBitmap = BitmapFactory.decodeFile(thumbFile.getAbsolutePath());
item.setIcon(newBitmap);
publishProgress();
}
else {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 14;
bitmap = ThumbnailUtils.createVideoThumbnail(item.getFilePath(), MediaStore.Video.Thumbnails.MINI_KIND);
newBitmap = ThumbnailUtils.extractThumbnail(bitmap, 64,64);
if(bitmap != null && !bitmap.isRecycled()){
bitmap = null;
}
if (newBitmap != null) {
item.setIcon(newBitmap);
thumbFile.createNewFile();
OutputStream out = new FileOutputStream(thumbFile);
newBitmap.compress(CompressFormat.JPEG, 26, out);
publishProgress();
Thread.sleep(200);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
public void onProgressUpdate(Void... value) {
FilesAdapter.this.notifyDataSetChanged();
}
}
//图片
class AsyncLoadImage extends AsyncTask@Override
protected Object doInBackground(FileItem... params) {
Bitmap bitmap;
Bitmap newBitmap;
FileItem item = params[0];
File thumbFile = new File(path + item.getFileName().replace(".", ""));
if (thumbFile.exists()) {
newBitmap = BitmapFactory.decodeFile(thumbFile.getAbsolutePath());
item.setIcon(newBitmap);
publishProgress();
}
else {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 14;
bitmap = BitmapFactory.decodeFile(item.getFilePath(), options);
newBitmap = ThumbnailUtils.extractThumbnail(bitmap, 64,64);
if(bitmap != null && !bitmap.isRecycled()){
bitmap = null;
// bitmap.recycle();
}
if (newBitmap != null) {
item.setIcon(newBitmap);
thumbFile.createNewFile();
OutputStream out = new FileOutputStream(thumbFile);
newBitmap.compress(CompressFormat.JPEG, 26, out);
publishProgress();
Thread.sleep(200);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
public void onProgressUpdate(Void... value) {
FilesAdapter.this.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Object result) {
}
}
//apk文件
class AsyncLoadApkicon extends AsyncTask@Override
protected Object doInBackground(FileItem... params) {
Bitmap bm;
FileItem item = params[0];
File thumbFile = new File(path + item.getFileName().replace(".", ""));
if (thumbFile.exists()) {
bm = BitmapFactory.decodeFile(thumbFile.getAbsolutePath());
item.setIcon(bm);
publishProgress();
}
else {
try {
Drawable dw = Helper.showUninstallAPKIcon(mContext, item.getFilePath());
if(dw!=null){
BitmapDrawable bd = (BitmapDrawable)dw;
bm = bd.getBitmap();
item.setIcon(bm);
publishProgress();
Thread.sleep(200);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
@Override
public void onProgressUpdate(Void... value) {
FilesAdapter.this.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Object result) {
}
}
////////////////////////////////////////////
/**
* 用来得到没有安装上的应用程序的icon
*/
public static Drawable showUninstallAPKIcon(Context context, String apkPath) {
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 {
return appInfo.loadIcon(pm);
} catch (OutOfMemoryError e) {
Log.e("ApkIconLoader", e.toString());
}
}
return null;
}
}
更多相关文章
- android videoView播放视频,对播放结束的监听方法
- java|android加载src路径下面的图片文件
- Android神奇之从服务器获取最新的视频资讯
- 基于Arduino的视频监控小车
- 第二十篇 Android获取本机图片、音频、视频、文档以及本地文件夹
- android视频截屏&手机录屏实现代码
- Android 使用 mp4parser 做视频裁剪
- Android 平台上拍照/录像,图片/视频选择,编辑和压缩的一站式解决方
- Android 自定义文件路径选择器