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;
    }
}

更多相关文章

  1. android videoView播放视频,对播放结束的监听方法
  2. java|android加载src路径下面的图片文件
  3. Android神奇之从服务器获取最新的视频资讯
  4. 基于Arduino的视频监控小车
  5. 第二十篇 Android获取本机图片、音频、视频、文档以及本地文件夹
  6. android视频截屏&手机录屏实现代码
  7. Android 使用 mp4parser 做视频裁剪
  8. Android 平台上拍照/录像,图片/视频选择,编辑和压缩的一站式解决方
  9. Android 自定义文件路径选择器

随机推荐

  1. 笔记:Android(安卓)Studio发布项目到Bintr
  2. Android编译系统详解(三)
  3. android studio3导入opencv4人脸识别例程
  4. Android(安卓)Socket Posix 实现
  5. Android(安卓)4.4报错,Android(安卓)7.0运
  6. Android传感器
  7. layout_gravity="bottom" 失效
  8. Anko:Android(安卓)代码动态布局的新方案
  9. android ping 网络延迟
  10. android给listview设置分割线Divider样式