android实现视频图片取缩略图
取缩略图不等同于缩放图片.
缩放图片是保持不失真的情况下缩放处理,并进行平滑处理.
缩略图则不然,允许失真,目的只是取出图片的轮廓.
保存Bitmap图片
private void saveBitmap(Bitmap bm,String picName) { Log.e(TAG, "保存图片"); String icon_dir=mMedialist.get(mMedialist.size()-1).getIconpath().replace("/", "__"); // String icon_dir=picName.replace("/", "__"); File dir = new File(Environment.getExternalStorageDirectory().toString()+"/movdo/clip",icon_dir ); if(!dir.exists()) if(!dir.mkdir()) Log.e(TAG, "don't create dir"); else Log.i(TAG, "DIR is created"); picName=picName.replace(".mp4", ".jpg"); File f = new File(dir.getAbsolutePath(), "0.jpg"); if (f.exists()) { f.delete(); } try { FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.PNG, 90, out); out.flush(); out.close(); Log.i(TAG, "已经保存"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
为图片取缩略图
public Bitmap getImageThumbnail(String filename,Bitmap bmp,int width,int height) { Bitmap bitmap=ThumbnailUtils.extractThumbnail(bmp, 300, 300); saveBitmap(bitmap,filename); return bitmap; }
为视频取缩略图
private Bitmap getVideoThumbnail(String videoPath, int width , int height){ Bitmap bitmap = null; bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, Thumbnails.MINI_KIND); bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); String filename=new String(); saveBitmap(bitmap,mMedialist.get(mMedialist.size()-1).getItemname()); return bitmap; }
根据路径解析进Bitmap
Bitmap thumb = BitmapFactory.decodeFile(filepath); getImageThumbnail(filename,thumb,300,300);
生活的残酷,让我们习惯了忘记疲倦,一直奔向远方,追寻着自己的梦想。