获取SD卡、T卡以及手机内存中的视频缩略图
来源:互联网 发布:知乎添加剂的危害 编辑:程序博客网 时间:2024/05/16 04:43
1.项目中使用listView展示SD卡中所有的视频,原来是需要手动添加路径,特别麻烦,有的外置卡什么的会读取不到,所以重新对方法进行了修改。
2.在adapter中设置图片的时候:
mViewHolder.videoView.setImageBitmap(bitmapCache.getVideoThumbnail(mContext,url, 500, 300, MediaStore.Images.Thumbnails.MICRO_KIND));
url = localPath.get(position);
3.数据来源:获取所有视频地址的方法。获取的数据添加到adapter 中的List<String> localPath;
private static List<String> list = new ArrayList<String>();private String[] strings;public String[] getPaths() { if (list != null) { list.clear(); } if (getContext() != null) { Cursor cursor = getActivity().getContentResolver().query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER); if (cursor != null) { while (cursor.moveToNext()) { String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); list.add(path); } cursor.close(); } } if (list.size() != 0) { strings = list.toArray(new String[list.size()]); } return strings;}
4.方法调用:
主要就是获取地址最后的"/"以及"."中间的名称命名为".jpg“。 如果没有就从新保存一次图片。有的视频可能解码的时候没有图片,这是需要设置默认一张图。
public static Bitmap getVideoThumbnail(Context context, String videoPath, int width, int height, int kind) { Bitmap bitmap = null; int start = videoPath.lastIndexOf("/") + 1; String path = SdcardHelper.SDCARD_DIR + videoPath.substring(start, videoPath.lastIndexOf(".")) + ".jpg"; bitmap = getBitmap(path,width, height); if (bitmap == null) { bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind); bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); if (bitmap == null) { bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.film_cover_loading1); } else { saveBitmap(bitmap, videoPath.substring(start, videoPath.lastIndexOf("."))); } } return bitmap;}
// 从SD卡中加载图片 public static Bitmap getBitmap(String path,int width, int height) { File mfile = new File(path); Bitmap bm = null; if (mfile.exists()) { Options options = new Options(); options.inJustDecodeBounds = true; options.inPurgeable = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, width, height); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); } return bm; }// 保存图片 private static File f; public static void saveBitmap(Bitmap bitmap, String picName) { f = new File(SdcardHelper.SDCARD_DIR, picName + ".jpg"); try { FileOutputStream out = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
0 0
- 获取SD卡、T卡以及手机内存中的视频缩略图
- Android获取SD卡上视频缩略图
- android 通过文件名获取SD卡视频缩略图
- Android获取SD卡上图片和视频的缩略图
- 获取SD卡视频文件缩略图
- android 扫描手机内存和SD卡,获取手机的视频、音频文件。把获取不到的文件扫描出来
- android 获取手机内存及SD卡内存可用空间
- 获取手机内存大小以及获取SD卡内存大小
- Android_获取手机上SD卡和内存中的可用空间的大小
- android保存数据到手机内存以及SD卡
- Android获取SD卡上图片和视频缩略图的几个例子
- Android获取SD卡上所有图片、视频缩略图和音乐专辑封面
- Android获取SD卡上所有图片、视频缩略图和音乐专辑封面
- Android studio 获取sd卡的图片和视频的缩略图
- Android获取SD卡上所有图片、视频缩略图和音乐专辑封面
- android 获取手机和SD卡可用内存
- 获取Android手机中SD卡内存信息
- 获取Android手机中SD卡内存信息
- Handler 小结
- 断言assert引入包
- MySQL:UNIX_TIMESTAMP函数不走索引问题
- isAssignableFrom和isInstance
- android Service的学习
- 获取SD卡、T卡以及手机内存中的视频缩略图
- Editplus格式化代码
- ios获取所有相册的视频并播放
- 购物车数据模型(相同商品id,订单数量相加,数组改变)
- __align(num) 分析
- ERROR YarnScheduler: Lost executor
- 调试eclipse javaEE所遇问题解决
- 概率问题的求解 —— 样本空间
- TextUtils