文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android开发之多媒体文件获取工具类实例【音频,视频,图片等】

2023-05-30 19:12

关注

本文实例讲述了Android开发之多媒体文件获取工具类。分享给大家供大家参考,具体如下:

package com.android.ocr.util;import java.io.File;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.database.Cursor;import android.graphics.Bitmap;import android.provider.MediaStore;public class MediaStoreUtil {  private static final String TAG = "MediaStoreUtil";    public static List<String> getAudioNames(Context context) {    List<String> list = new ArrayList<String>();    Cursor cursor = context.getContentResolver().query(        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,        new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE,            MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM,            MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE,            MediaStore.Audio.Media.DATA }, null, new String[] {}, null);    while (cursor.moveToNext()) {      String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));      list.add(fileName);    }    return list;  }    public static List<String> getImageNames(Context context) {    List<String> list = new ArrayList<String>();    Cursor cursor = context.getContentResolver().query(        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,        new String[] { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.TITLE,            MediaStore.Images.Media.MIME_TYPE, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATA }, null,        new String[] {}, null);    while (cursor.moveToNext()) {      Logger.i(TAG, "filePath==" + MediaStore.Images.Media.DATA);      String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));      String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));      list.add(filePath + "/" + fileName);    }    return list;  }    public static List<File> getImages(Context context) {    List<File> list = new ArrayList<File>();    Cursor cursor = context.getContentResolver().query(        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,        new String[] { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.TITLE,            MediaStore.Images.Media.MIME_TYPE, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATA }, null,        new String[] {}, null);    while (cursor.moveToNext()) {      String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));      Logger.i(TAG, "filePath==" + filePath);      String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));      //Logger.i(TAG, "fileName==" + fileName);      File file = new File(filePath);      list.add(file);    }    return list;  }    public static List<File> getAllFiles(Context context) {    List<File> list = new ArrayList<File>();    Cursor cursor = context.getContentResolver().query(        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,        new String[] { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.TITLE,            MediaStore.Images.Media.MIME_TYPE, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATA }, null,        new String[] {}, null);    while (cursor.moveToNext()) {      String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));      Logger.i(TAG, "filePath==" + filePath);      String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));      //Logger.i(TAG, "fileName==" + fileName);      File file = new File(filePath);      list.add(file);    }    return list;  }    public static Bitmap[] getBitmaps(Context context) {    Bitmap[] bitmaps;    String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null,        MediaStore.Images.Media._ID);    int count = cursor.getCount();    int image_column_index = cursor.getColumnIndex(MediaStore.Images.Media._ID);    bitmaps = new Bitmap[count];    for (int i = 0; i < count; i++) {      cursor.moveToPosition(i);      int id = cursor.getInt(image_column_index);      bitmaps[i] = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(), id,          MediaStore.Images.Thumbnails.MICRO_KIND, null);    }    return bitmaps;  }    public static List<String> getThumbNames(Context context) {    List<String> list = new ArrayList<String>();    Cursor cursor = context.getContentResolver().query(        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,        new String[] { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.KIND,            MediaStore.Images.Thumbnails.IMAGE_ID }, null, new String[] {}, null);    while (cursor.moveToNext()) {      String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));      list.add(fileName);    }    return list;  }    public static ArrayList<VideoInfo> getVideoInfo(Context context){    String[] thumbColumns = new String[]{        MediaStore.Video.Thumbnails.DATA,        MediaStore.Video.Thumbnails.VIDEO_ID    };    String[] mediaColumns = new String[]{        MediaStore.Video.Media.DATA,        MediaStore.Video.Media._ID,        MediaStore.Video.Media.TITLE,        MediaStore.Video.Media.MIME_TYPE    };    //首先检索SDcard上所有的video    Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);    ArrayList<VideoInfo> videoList = new ArrayList<VideoInfo>();    if(cursor.moveToFirst()){      do{        VideoInfo info = new VideoInfo();        info.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));        info.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));        info.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));        //获取当前Video对应的Id,然后根据该ID获取其Thumb        int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));        String selection = MediaStore.Video.Thumbnails.VIDEO_ID +"=?";        String[] selectionArgs = new String[]{            id+""        };        Cursor thumbCursor = context.getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);        if(thumbCursor.moveToFirst()){          info.thumbPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA));        }        //然后将其加入到videoList        videoList.add(info);      }while(cursor.moveToNext());    }    return videoList;  }  static class VideoInfo{    String filePath;    String mimeType;    String thumbPath;    String title;  }}

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯