码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-04-06 13:07:27      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

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开发之获取本地视频和获取自拍视频

标签:

原文地址:http://www.cnblogs.com/chenxibobo/p/5358552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!