How Android Gets the multimedia files on the SD card

Source: Internet
Author: User

Get video files:

Contentresolver contentresolver = Mcontext.getcontentresolver ();
string[] projection = new String[]{mediastore.video.media.title};
cursor cursor = contentresolver.query (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection,
NULL, NULL, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
Cursor.movetofirst ();
int filenum = Cursor.getcount ();

for (int counter = 0; counter < FileNum; counter++) {
LOG.W (TAG, "----------------------file is:" + cursor.getstring (Cursor.getcolumnindex (MediaStore.Video.Media.TITLE) ) );

Cursor.movetonext ();
}
Cursor.close ();

If you get the path, you need to say projection modified to string[] projection = new String[]{mediastore.video.media.data};

The obtained statement also needs to be modified to: cursor.getstring (Cursor.getcolumnindex (MediaStore.Video.Media.DATA));

Get audio files:

cursor cursor = mcontext.getcontentresolver (). Query (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
NULL, NULL, NULL, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
Cursor.movetofirst ();
int counter = Cursor.getcount ();
String title = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.TITLE));

LOG.W (TAG, "------------before looping, title =" + title);
for (int j = 0; J < counter; J + +) {
LOG.W (TAG, "-----------title ="
+ cursor.getstring (Cursor.getcolumnindex (MediaStore.Audio.Media.TITLE)));

Cursor.movetonext ();

}
Cursor.close ();

The following is from the network http://blog.sina.com.cn/s/blog_5be1061c0100ctco.html

First of all, let's talk about the Android multimedia database. Mediastore This class is a multimedia database provided by the Android system, which can be extracted from the Android multimedia information. This mediastore includes all the information in the multimedia database, including the audio

, video and images, Android put all the multimedia database interface encapsulated, all the databases do not have to create their own, the direct call to take advantage of the contentresolver removed with those packaged interface can be used for database operations. Today I'll introduce some

The use of these interfaces.

First, to get a contentresolver instance, Contentresolver can take advantage of an activity or service context. As shown below:

Contentresolver mresolver = Ctx.getcontentresolver ();

The top of the CTX is a context,activity.this is that context, this context is equivalent to a contextual environment. Once you get this context, you can call the Getcontentresolver interface to get the Contentresolver instance.

Contentresolver instances obtained, you can make a variety of queries, the following I take the audio database as an example to explain the method of adding and deleting, video and image and audio very similar.

Before explaining the various queries, I'll show you how to see which multimedia tables are available on Android. In the ADB shell, locate the/data/data/com.android.providers.media/databases/, and then locate the SD card database file (typically

A. db file), and then enter the command sqlite3 the name of the database to query the Android multimedia database. The table command lists all the tables for the multimedia database. Scheme plus the table name can query all column names in the table. Here you can benefit

Use SQL statements to see the data you want, and remember to keep in mind that each statement is preceded by a semicolon. Here's how to add and revise these tables.

Query, the code looks like this:

cursor cursor = resolver.query (_uri, Prjs, selections, Selectargs, order);

The Contentresolver Query method accepts several parameters with the following parameter meanings:

URI: This URI represents the name of the database to be queried plus the name of the table. This URI is generally obtained directly from the Mediastore, for example, I want to take all the information of the song, I must use the MediaStore.Audio.Media. EXTERNAL _content_uri this URI.

Album information to use MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI this URI to query, and other queries are similar.

PRJS: This parameter represents the column to select from the table, represented by a string array.

Selections: equivalent to the WHERE clause in the SQL statement, which represents your query criteria.

Selectargs: Is this the argument that you have in your selections? This symbol is where the actual value can be substituted for this question mark. What if selections this? , then this string array can be null.

Order: Describes what the query results are sorted by.

Above is the meaning of each parameter, it returns the query results a cursor, the cursor is equivalent to the database query result, usage and it is similar.

--------------------------------------------------------------------------------------------------------------- ----

Add the code as follows:

Contentvalues values = new Contentvalues ();

Values.put (mediastore.audio.playlists.members.play_order,0);

Resolver.insert (_uri, values);

This insert passes only two parameters, one is the URI (the same URI as the query), and the other is contentvalues. This contentvaluses corresponds to a row of data in the database, as long as the put method to set each column, directly using the insert side

It's good to have the law inserted.

Updated with the following code:

Contentresolver resolver = Ctx.getcontentresolver ();

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

Contentvalues values = new Contentvalues ();

Values.put (MediaStore.Audio.Media.DATE_MODIFIED, sid);

Resolver.update (Mediastore.audio.playlists.external_content_uri,values, where, Selectionargs);

The above Update method and query also has an increase in the parameters are very similar, here will not repeat the narrative, you can also directly refer to Google's documentation, there is also written clearly.

Delete the code as follows:

Contentresolver resolver = Ctx.getcontentresolver ();

nbsp Resolver.delete (Mediastore.audio.playlists.external_content_uri,where, Selectionargs);

How Android Gets the multimedia files on the SD card

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.