获取SD卡、T卡以及手机内存中的视频缩略图

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值