SoFunction
Updated on 2025-03-01

Android development multimedia file acquisition tool examples [audio, video, pictures, etc.]

This article describes the multimedia file acquisition tool class for Android development. Share it for your reference, as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
  * Query information based on MediaStore
  * @Project App_ReadCard
  * @Package
  * @author chenlin
  * @version 1.0
  * @Date June 16, 2013
  * @Note TODO
  */
public class MediaStoreUtil {
  private static final String TAG = "MediaStoreUtil";
  /**
    * Query audio file name
    *
    * @param context
    * @return
    */
  public static List<String> getAudioNames(Context context) {
    List<String> list = new ArrayList<String>();
    Cursor cursor = ().query(
        .EXTERNAL_CONTENT_URI,
        new String[] { ._ID, .DISPLAY_NAME, ,
            , , ,
            , .MIME_TYPE, ,
             }, null, new String[] {}, null);
    while (()) {
      String fileName = ((.DISPLAY_NAME));
      (fileName);
    }
    return list;
  }
  /**
    * Query the image file name
    *
    * @param context
    * @return
    */
  public static List<String> getImageNames(Context context) {
    List<String> list = new ArrayList<String>();
    Cursor cursor = ().query(
        .EXTERNAL_CONTENT_URI,
        new String[] { ._ID, .DISPLAY_NAME, ,
            .MIME_TYPE, ,  }, null,
        new String[] {}, null);
    while (()) {
      (TAG, "filePath==" + );
      String filePath = (());
      String fileName = ((.DISPLAY_NAME));
      (filePath + "/" + fileName);
    }
    return list;
  }
  /**
    * Query picture files
    *
    * @param context
    * @return
    */
  public static List<File> getImages(Context context) {
    List<File> list = new ArrayList<File>();
    Cursor cursor = ().query(
        .EXTERNAL_CONTENT_URI,
        new String[] { ._ID, .DISPLAY_NAME, ,
            .MIME_TYPE, ,  }, null,
        new String[] {}, null);
    while (()) {
      String filePath = (());
      (TAG, "filePath==" + filePath);
      String fileName = ((.DISPLAY_NAME));
      //(TAG, "fileName==" + fileName);
      File file = new File(filePath);
      (file);
    }
    return list;
  }
  /**
    * Query file
    *
    * @param context
    * @return
    */
  public static List<File> getAllFiles(Context context) {
    List<File> list = new ArrayList<File>();
    Cursor cursor = ().query(
        .EXTERNAL_CONTENT_URI,
        new String[] { ._ID, .DISPLAY_NAME, ,
            .MIME_TYPE, ,  }, null,
        new String[] {}, null);
    while (()) {
      String filePath = (());
      (TAG, "filePath==" + filePath);
      String fileName = ((.DISPLAY_NAME));
      //(TAG, "fileName==" + fileName);
      File file = new File(filePath);
      (file);
    }
    return list;
  }
  /**
    * Get all the highlights
    *
    * @param context
    * @return
    */
  public static Bitmap[] getBitmaps(Context context) {
    Bitmap[] bitmaps;
    String[] projection = { ._ID,  };
    Cursor cursor = ().query(.EXTERNAL_CONTENT_URI, projection, null, null,
        ._ID);
    int count = ();
    int image_column_index = (._ID);
    bitmaps = new Bitmap[count];
    for (int i = 0; i < count; i++) {
      (i);
      int id = (image_column_index);
      bitmaps[i] = ((), id,
          .MICRO_KIND, null);
    }
    return bitmaps;
  }
  /**
    * Query the name of the image column file
    *
    * @param context
    * @return
    */
  public static List<String> getThumbNames(Context context) {
    List<String> list = new ArrayList<String>();
    Cursor cursor = ().query(
        .EXTERNAL_CONTENT_URI,
        new String[] { ._ID, , ,
            .IMAGE_ID }, null, new String[] {}, null);
    while (()) {
      String fileName = ((.DISPLAY_NAME));
      (fileName);
    }
    return list;
  }
  /**
    * Get all video files
    * @param context
    */
  public static ArrayList<VideoInfo> getVideoInfo(Context context){
    String[] thumbColumns = new String[]{
        ,
        .VIDEO_ID
    };
    String[] mediaColumns = new String[]{
        ,
        ._ID,
        ,
        .MIME_TYPE
    };
    //First search all videos on SDcard    Cursor cursor = ().query(.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
    ArrayList<VideoInfo> videoList = new ArrayList<VideoInfo>();
    if(()){
      do{
        VideoInfo info = new VideoInfo();
         = (());
         = ((.MIME_TYPE));
         = (());
        //Get the ID corresponding to the current Video, and then obtain its Thumb based on the ID        int id = ((._ID));
        String selection = .VIDEO_ID +"=?";
        String[] selectionArgs = new String[]{
            id+""
        };
        Cursor thumbCursor = ().query(.EXTERNAL_CONTENT_URI, thumbColumns, selection, selectionArgs, null);
        if(()){
           = (());
        }
        // Then add it to the videoList        (info);
      }while(());
    }
    return videoList;
  }
  static class VideoInfo{
    String filePath;
    String mimeType;
    String thumbPath;
    String title;
  }
}

For more information about Android related content, please check out the topic of this site:Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android file operation skills summary》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.