欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2020-11-30 13:55:44|栏目:Android代码|点击:

本文实例讲述了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;
/**
 * 根据MediaStore查询信息
 * @Project  App_ReadCard
 * @Package  com.android.ocr.util
 * @author   chenlin
 * @version  1.0
 * @Date    2013年6月16日
 * @Note    TODO
 */
public class MediaStoreUtil {
  private static final String TAG = "MediaStoreUtil";
  /**
   * 查询音频文件名称
   *
   * @param context
   * @return
   */
  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;
  }
  /**
   * 查询图片文件名称
   *
   * @param context
   * @return
   */
  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;
  }
  /**
   * 查询图片文件
   *
   * @param context
   * @return
   */
  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;
  }
  /**
   * 查询文件
   *
   * @param context
   * @return
   */
  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;
  }
  /**
   * 获取所有的缩列图
   *
   * @param context
   * @return
   */
  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;
  }
  /**
   * 查询图片缩列文件名称
   *
   * @param context
   * @return
   */
  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;
  }
  /**
   * 获得所有视频文件
   * @param context
   */
  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;
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android文件操作技巧汇总》、《Android资源操作技巧汇总》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

上一篇:Android利用动画实现背景逐渐变暗

栏    目:Android代码

下一篇:快速处理ListView为空的情况

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

本文地址:http://www.codeinn.net/misctech/28023.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有