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)