Android读取手机上的多媒体文件


public class MediaBrowserUtil
{
    private static final String TAG = MediaBrowserUtil.class.getSimpleName();
    private static List<String> videoFormatList = new ArrayList<>();

    static
    {
        videoFormatList.add("mp4");
        videoFormatList.add("avi");
        videoFormatList.add("mov");
        videoFormatList.add("mkv");
        videoFormatList.add("3gp");
    }

    public static void queryImages()
    {
        final ContentResolver mContentResolver = MyApplication.getInstance().getApplicationContext().getContentResolver();
        PhotoBean photoBean;
        Config.photoBeanList.clear();
        String[] projection = {
                "_id",
                MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATE_TAKEN,
                MediaStore.Images.Media.SIZE};

        Cursor mCursor = mContentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, "("
                        + MediaStore.Images.Media.MIME_TYPE + "=? or "
                        + MediaStore.Images.Media.MIME_TYPE + "=? or "
                        + MediaStore.Images.Media.MIME_TYPE + "=? or "
                        + MediaStore.Images.Media.MIME_TYPE + "=? ) and _size > 0",
                new String[]{"image/jpg", "image/png", "image/jpeg"},
                MediaStore.Images.Media.DATE_TAKEN + " desc");

        Config.photoBeanList.clear();

        while (mCursor.moveToNext())
        {
            photoBean = new PhotoBean();
            int id = mCursor.getInt(mCursor.getColumnIndex("_id"));
            String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            String display_name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
            long datetaken = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN));
            int size = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE));

            Log.e(TAG, "queryImages(), path=" + path);

            if (path != null && size > 0)
            {
                photoBean.setImage_id(id);
                photoBean.setImage_path(path);
                photoBean.setImage_name(display_name);
                photoBean.setImage_date(datetaken);
                photoBean.setImage_size(size);
                ClientConfig.photoBeanList.add(photoBean);
            }

        }

        if (mCursor != null)
        {
            mCursor.close();
        }
    }

    /**
     * 支持的视频格式: MP4、AVI、MOV、MKV、3gp
     * */
    public static void queryVideos()
    {
        final Context mContext = MyApplication.getInstance().getApplicationContext();
        VideoBean videoBean = null;
        Cursor thumbCursor = null;
        String[] projection = {
                "_id",
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.TITLE,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.DATE_TAKEN,
                MediaStore.Video.Media.SIZE};

        String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
                MediaStore.Video.Thumbnails.VIDEO_ID};

        ContentResolver mContentResolver = mContext.getContentResolver();

        Cursor mCursor = mContentResolver.query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                projection,
                "mime_type like \"video/%\" and _size > 0 ",
                null,
                MediaStore.Images.Media.DATE_TAKEN + " desc");

        if (mCursor == null)
        {
            Log.e(TAG, "queryVideos() failed. mCursor=null");
            return;
        }

        int count = mCursor.getCount();
        Log.e(TAG, "queryVideos(). mCursor.getCount()=" + count);
        if (count <= 0)
        {
            Log.e(TAG, "queryVideos() failed. mCursor.getCount()<=0");
            return;
        }

        Config.videoBeanList.clear();

        while (mCursor.moveToNext())
        {
            int id = mCursor.getInt(mCursor.getColumnIndex("_id"));
            String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA));
            String display_name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
            String title = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.TITLE));
            int duration = mCursor.getInt(mCursor .getColumnIndex(MediaStore.Video.Media.DURATION));
            //long datetaken = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN));

            if (duration <= 1000)
            {
                Log.e(TAG, "queryVideos() error. duration <= 0");
                continue;
            }

            if (path == null)
            {
                Log.e(TAG, "queryVideos() error. path == null");
                continue;
            }

            int lastIndex = path.lastIndexOf(".");
            if (lastIndex < 0)
            {
                Log.e(TAG, "queryVideos() error.lastIndex < 0");
                continue;
            }

            String suffix = path.substring(lastIndex + 1, path.length());
            if (suffix == null)
            {
                Log.e(TAG, "queryVideos() error.suffix == null");
                continue;
            }
            suffix = suffix.toLowerCase();
            Log.e(TAG, "queryVideos().suffix =" + suffix + "; is_mp4=" + suffix.equals("mp4")
                    + "; is_avi=" + suffix.equals("avi") + "; is_mov=" + suffix.equals("mov")
                    + "; is_mkv=" + suffix.equals("mkv") + "; is_3gp=" + suffix.equals("3gp"));

            if (videoFormatList.contains(suffix))
            {
                videoBean = new VideoBean();

                videoBean.setVideo_id(id);
                videoBean.setVideo_path(path);
                videoBean.setVideo_displayName(display_name);
                videoBean.setVideo_title(title);
                videoBean.setVideo_duration(duration);

                ClientConfig.videoBeanList.add(videoBean);
                Log.e(TAG, "queryVideos() success.");
            }
            else
            {
                Log.e(TAG, "queryVideos() error.video format is not supported.");
            }
        }

        if (thumbCursor != null)
        {
            thumbCursor.close();
        }

        if (mCursor != null)
        {
            mCursor.close();
        }
    }

    public static void queryAudios()
    {
        final Context mContext = MyApplication.getInstance().getApplicationContext();
        AudioBean audioBean;
        String[] projection = {
                "_id",
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION};

        ContentResolver mContentResolver = mContext.getContentResolver();
        Cursor mCursor = mContentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        Config.audioBeanList.clear();
        while (mCursor.moveToNext())
        {
            int id = mCursor.getInt(mCursor.getColumnIndex("_id"));
            String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String display_name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
            String title = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String artist = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String album = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
            int duration = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

            if (duration > 5000 && path != null && path.toLowerCase().endsWith(".mp3"))
            {
                audioBean = new AudioBean();
                audioBean.setAudio_id(id);
                audioBean.setAudio_path(path);
                audioBean.setAudio_display_name(display_name);
                audioBean.setAudio_title(title);
                audioBean.setAudio_artist(artist);
                audioBean.setAudio_album(album);
                audioBean.setAudio_duration(duration);
                ClientConfig.audioBeanList.add(audioBean);
            }
        }

        if (mCursor != null)
        {
            mCursor.close();
        }
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LakeSideHu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值