博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取SD卡、T卡以及手机内存中的视频缩略图
阅读量:3762 次
发布时间:2019-05-22

本文共 3083 字,大约阅读时间需要 10 分钟。

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
list = new ArrayList
();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();        }

转载地址:http://hbfpn.baihongyu.com/

你可能感兴趣的文章
淘宝购物车测试用例
查看>>
Java语言基础(多态,抽象类,接口)
查看>>
Java语言基础(内部类,匿名内部类,object类)
查看>>
Java语言基础(数组冒泡排序,选择排序等,二分法)
查看>>
史上最全的集合(集合UML图(Collection集合和Map集合)详解,子接口(list和set)泛型)
查看>>
IO流(字节流和字符流)
查看>>
P1563 玩具谜题
查看>>
L1-002 打印沙漏 (20分)
查看>>
P1217 [USACO1.5]回文质数 Prime Palindromes
查看>>
P1014 Cantor表
查看>>
实验十 算术编码
查看>>
实验二 二维随机变量信息量的计算
查看>>
使用react脚手架创建react项目时发生错误
查看>>
关于setState是异步与同步的
查看>>
56. 合并区间---js解法
查看>>
5. 最长回文子串---js解法
查看>>
USACO 2007 Open Gold/acwing2240:餐饮 (拆点+最大流)‘三分图匹配’
查看>>
那些年你不知道的C++STL进制转换函数
查看>>
区间和并问题 思路加模板整理(校门外的树)
查看>>
C++中next_permutation函数的使用方法、原理及手动实现
查看>>