Player preparation notes ---- mediastore

Source: Internet
Author: User

In my initial conception, I used the file class to recursively traverse all the files in the SD card, and then filtered out ". put the file ending in MP3 "format into a map. then, put the map into the list to load the media.

However, it is found that this method takes a long time and is prone to "getting stuck". Then I want to save the list permanently to the local device. However, if there are hundreds of songs, the file is too large, memory controls are very occupied. after reading some materials, I found that the Android mobile phone has a media library, which can be directly queried. after multiple attempts, the file is successfully read. take a note here.


1. Obtain the contextresolver instance;

Method: contexrresolver Cr = context. getcontextresolver ();

Explanation: context indicates the context of activity. This, which is the context in the activity to be operated.

After you use the getcontextresolver (); method to obtain the contextresolver object, you can directly query it.


2. Use the cursor object;

Method:

1, cursor c = Cr. getcontentresolver (). Query (Uri, projection, selection, selectionargs, sortorder)

The CR here is the previously obtained contextresolver object. In this way, the contextresolver object must be instantiated first.

So, can the instance come out directly? Yes ~

2, cursor c = context. getcontentresolver (). Query (Uri, projection, selection, selectionargs, sortorder)

You only need to use context. getcontextresolver () directly. There are no errors in the two methods. use different methods according to the actual situation.

Parameter meaning:

Uri: specify the name of the database to be queried and the name of the table. You can find parameters for the relevant information in mediastore. For details, see the development documentation.
Projection: Specifies the columns in the database table to be queried. The returned cursor contains the corresponding information. Null returns all information.
Selection: Specify query Conditions
Selectionargs: Which of the following are in the selection parameter? This symbol is used to replace the question mark with the actual value. If selection does not exist? Then, the string array can be null.
Sortorder: Specify the order of query results


3. mediastore Database

Subject sentence:

Mediastore. Audio: stores Audio Information. mediastore. Image: Stores image information. mediastoue. Video: stores video information.

These three are the basic information of the three major files, and there are many methods and parameters behind each class. Currently I am using audio information, and other methods are useless. I think the method should be similar.

1. mediastore. Audio query parameters:

First, we need to query the media file, which is the media class. Therefore, we need to add media after the subject sentence.

Changed to: mediastore. Audio. Media;

Well, now we want to query some media information. Here I have collected some common attributes for your reference:

Mediastore. Audio. Media. _ id; // internal ID

Mediastore. Audio. Media. display_name; // music name

Mediastore. Audio. Media. Title; // music title

Mediastore. Audio. Media. Artist; // artist

Mediastore. Audio. Media. album; // album

Mediastore. Audio. Media. duration; // music duration

Mediastore. Audio. Media. Data; // music path

These attributes are commonly used in Query files. How can we use these attributes?

I found two ways to query:

First:

Here I am using to directly put the contextresolver object in the statement.

When getting the music duration, note that the duration here is not converted, and I do not know whether it is in seconds or milliseconds. It is printed out with a long string. then, we need to convert:

Public class tools {/*** conversion time ** @ Param time * @ return */public static string formattime (long time) {string min = Time/(1000*60) + ""; string sec = time % (1000*60) + ""; if (min. length () <2) {min = "0" + time/(1000*60) + "";} else {min = Time/(1000*60) + "";} If (sec. length () = 4) {SEC = "0" + (time % (1000*60) + "";} else if (sec. length () = 3) {SEC = "00" + (time % (1000*60) + "";} else if (sec. length () = 2) {SEC = "000" + (time % (1000*60) + "";} else if (sec. length () = 1) {SEC = "0000" + (time % (1000*60) + "";} return min + ":" + sec. trim (). substring (0, 2 );}}

Ah ~ I did not write it. I copied it online. Hahaha. You can see that I used it before I got the length.

The first method is clearer.

Public static list <Map <string, string> findmusicfileusecuesor (context) {cursor = context. getcontentresolver (). query (mediastore. audio. media. external_content_uri, null, mediastore. audio. media. default_sort_order); List <Map <string, string> List = new arraylist <Map <string, string> (); If (cursor. movetofirst () {do {Map <string, string> map = new hashmap <string, string> (); // obtain the music idmap. put ("_ id", cursor. getstring (cursor. getcolumnindexorthrow (mediastore. audio. media. _ id )). tostring (); // obtain the music name map. put ("name", cursor. getstring (cursor. getcolumnindexorthrow (mediastore. audio. media. display_name); // obtain the music title map. put ("tittle", cursor. getstring (cursor. getcolumnindexorthrow (mediastore. audio. media. title); // obtain the map of the Music Artist. put ("artist", cursor. getstring (cursor. getcolumnindexorthrow (mediastore. audio. media. artist); // obtain the Music Album map. put ("album", cursor. getstring (cursor. getcolumnindexorthrow (mediastore. audio. media. album); // obtain the music duration map. put ("duration", tools. formattime (cursor. getlong (cursor. getcolumnindexorthrow (mediastore. audio. media. duration); // obtain the music path map. put ("path", cursor. getstring (cursor. getcolumnindexorthrow (mediastore. audio. media. data); list. add (MAP);} while (cursor. movetonext ();} If (list. size ()> 0) {for (INT I = 0; I <list. size (); I ++) {system. out. println ("list: ---->" + list. get (I ). tostring ();} return list;} return NULL ;}

Method 2:

public static List<Map<String, String>> findMusicFileFromPhone(Context context) {Cursor cursor = context.getContentResolver().query(Parameter.MEDIA_URI,Parameter.MEDIA_PROJECTION, Parameter.MEDIA_SELECTION,Parameter.MEDIA_SELECTIONARGS, null);List<Map<String, String>> musicDate = new ArrayList<Map<String, String>>();while (cursor.moveToNext()) {Map<String, String> map = new HashMap<String, String>();map.put("tittle", cursor.getString(0));map.put("artist", cursor.getString(1));map.put("durtion", tools.formatTime(cursor.getLong(2)));map.put("path", cursor.getString(3));musicDate.add(map);}if (musicDate.size() > 0) {return musicDate;} else {System.out.println("musicDate :" + musicDate.size());return null;}}

Parameters of the second method:

public static final Uri MEDIA_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;public static final String[] MEDIA_PROJECTION = {    MediaStore.Audio.Media.TITLE,    MediaStore.Audio.Media.ARTIST,    MediaStore.Audio.Media.DURATION,    MediaStore.Audio.Media.DATA };    public static final String[] MEDIA_SELECTIONARGS = { "audio/mpeg","audio/x-ms-wma" };public static final String MEDIA_SELECTION = MediaStore.Audio.Media.MIME_TYPE+ "=? or " + MediaStore.Audio.Media.MIME_TYPE + "=?";


I guess what the last two sentences mean, but I still hope Daniel can explain them...

In particular, the last sentence should be the filtering type. I can see it online "? "Can be defined by myself, I can change this code to report an error. I don't know why .... I tried. I have also tried MP3 and MP3 in lower case... =. = science popularization ..

In this way, I think it looks messy .. (Well, I did not write well ). 2. I always think it is not good to write dead data during writing, for example:

map.put("tittle", cursor.getString(0));

If the order of the parameters is changed, it will be awesome...

However, this method makes it easier to update, add, delete, and operate [email protected] ^ ....?

Okay, take notes here and study again .....

This article is from "My family has a doll" blog, please be sure to keep this http://66390099.blog.51cto.com/8948233/1558100

Player preparation notes ---- mediastore

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.