Android开发之获取本地视频和获取自拍视频

Posted chenxibobo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发之获取本地视频和获取自拍视频相关的知识,希望对你有一定的参考价值。

1、获取本地所有视频

    public void getLoadMedia() {
        Cursor cursor = UILApplication.instance.getApplicationContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
        try {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)); // id
                String displayName =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM)); // 专辑
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST)); // 艺术家
                String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)); // 显示名称
                String mimeType =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)); // 路径
                long duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)); // 时长
                long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)); // 大小
                String resolution =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.RESOLUTION));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
    }

2、获取拍照视频

public static final String[] videoCursorCols = new String[] { MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.ARTIST, MediaStore.Video.Media.ALBUM, MediaStore.Video.Media.RESOLUTION, MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DATA };

    public static Video fileUriForVideo(Activity activity, Uri uri) {

        Cursor cursor = activity.getContentResolver().query(uri, videoCursorCols, null, null, null);

        if (cursor == null || cursor.getCount() == 0) {
            return null;
        } else {

            Video video = new Video();
            cursor.moveToFirst();
            /* file name */
            if (cursor.getString(1) != null) {
                video.setName(cursor.getString(1));
            }
            /* media name */
            if (cursor.getString(2) != null) {
               video.setDisplayName(cursor.getString(2));
            }
            /* play duration */
            if (cursor.getString(3) != null) {
                video.setTimelong((cursor.getInt(3) / 1000) + "s");
            }
            /* artist */
            if (cursor.getString(4) != null) {
                video.setSinger(cursor.getString(4));
            }
            /* album */
            if (cursor.getString(5) != null) {
                video.setAlbum(cursor.getString(5));
            }
            /* resolution */
            if (cursor.getString(6) != null) {
                video.setResolution(cursor.getString(6));
            }
            /* media type */
            if (cursor.getString(7) != null) {
                video.setMimeType(cursor.getString(7).trim());
            }
            /* media size */
            if (cursor.getString(8) != null) {
                video.setDownfilesize(new java.text.DecimalFormat("#.00").format((cursor.getInt(8) / 1024.0 / 1024.0)) + " MB");

            }
            /* media file path */
            if (cursor.getString(9) != null) {
                video.setVoiceurl(cursor.getString(9));
            }
            if (cursor != null) {
                cursor.close();
            }
            return video;
        }
    }

 

以上是关于Android开发之获取本地视频和获取自拍视频的主要内容,如果未能解决你的问题,请参考以下文章

javaCV开发详解之10:基于dshow调用windows摄像头视频和音频,想要获取屏幕画面首选gdigrab

Android多媒体信息获取

安卓获取手机视频和图片

Android Studio App开发之使用摄像机录制视频和从视频库中选取视频的讲解及实战(附源码)

Android Framework实战开发视频--跨进程通信之课程介绍

Android Framework实战开发视频--跨进程通信之课程介绍