Introduction of SD card and system files based on Android scan _android

Source: Internet
Author: User
Tags addall
If you have done multimedia applications, will certainly be distressed, how to obtain the SD card multimedia files. Android is still very powerful, and if you know how to invoke the Android API, everything is OK.
When a cell phone or simulator is powered on, the Android Mediascanner is invoked to scan the SD card and the files in memory. Here is the log information.
Copy Code code as follows:

12-13 15:39:11.062:verbose/mediaplayerservice: Create New Media retriever from PID 349<br>
12-13 15:39:11.082:debug/mediascannerservice (349): Getdefaultlocale =ZH_CN
12-13 15:39:11.122:debug/surfaceflinger (102): Layer::requestbuffer (this=0x7c8c68), Index=1, pid=12866, w=309, h=192 Success
12-13 15:39:11.142:info/mediascanner (349): Moriginalcount = prune, Thumb flag false<br>
12-13 15:39:11.142:debug/mediascanner (349): Prescan time:44ms<br>
12-13 15:39:11.142:debug/mediascanner (349): Scan time:13ms<br>
12-13 15:39:11.142:debug/mediascanner (349): Postscan time:2ms<br>
12-13 15:39:11.142:debug/mediascanner (349): Total time:59ms<br>
12-13 15:39:11.152:debug/mediaprovider (349): Un-lock thumbnail worker<br>
12-13 15:39:11.152:debug/mediaprovider (349): Un-lock thumbnail worker<br>
12-13 15:39:11.182:debug/mediascannerservice (349): Done scanning Volume external

So where did the record of the scan go? Ha. Where do you think it is? Data/data/com.android.media/providers/databases/external
what information did it save, pull it out and see:

Then, we directly use ContentProvider can directly access to the SD card multimedia information, you still use to listfile it? Also use to resolve the information in the media file yourself (time length, filename, album name ...). Everything, huh?
Copy Code code as follows:

Cursor Cursor = Context.getcontentresolver (). Query (<BR> mediastore.audio.media.external_content_uri,<br > New string[] {mediastore.audio.media._id, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, <BR> MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, mediastore.audio.media.album,<br > MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, Mediastore.audio.media.data}<br>, "_SIZE>?", New string[]{1024*1024+ ""},null);

Okay, last question <BR> when you add some multimedia files to your SD card, Android doesn't automatically flush it into the database. So how do we get it to be refreshed manually, as follows:
Copy Code code as follows:

Intentfilter intentfilter = new Intentfilter (intent.action_media_scanner_started);
Intentfilter.addaction (intent.action_media_scanner_finished);
Intentfilter.adddatascheme ("file");
Scanreceiver = new Scansdfilesreceiver ();
Registerreceiver (Scanreceiver, Intentfilter);
Sendbroadcast (New Intent (intent.action_media_mounted, Uri.parse ("file://" + Environment.getexternalstoragedirectory ()));


Private class Scansdfilesreceiver extends Broadcastreceiver {
public void OnReceive (context context, Intent Intent) {
String action = Intent.getaction ();
if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals (ACTION)) {
Scanhandler.sendemptymessage (started);
}
if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals (ACTION)) {
Scanhandler.sendemptymessage (finished);
}
}
}


Private Handler Scanhandler = new Handler () {
public void Handlemessage (msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case started:
Mydialog Scandialog = new Mydialog (locallist.this);
Scanalertdialog = Scandialog.scanfile ();
Scanalertdialog.show ();
LOG.I (TAG, "showing");
Break
Case finished:
arraylist<song> tempsongs = READFILELIST.READDATAFROMSD (locallist.this, local);
if (tempsongs!= null && tempsongs.size () >0) {
if (songs!= null && songs.size () >0) {
Songs.clear ();
Songs.addall (tempsongs);
Songadapter.notifydatasetchanged ();
}else {
Songs = new Arraylist<song> ();
Songs.addall (tempsongs);
Initsong_lv ();
}
}else {
Toast.maketext (Locallist.this, "No songs in SD card, please add after Scan", Toast.length_short). Show ();
}
LOG.I (TAG, "Finish");
if (Scanalertdialog!=null && scanalertdialog.isshowing ()) {
Scanalertdialog.dismiss ();
}
Unregisterreceiver (Scanreceiver);
Break
Case DISMISS:
LOG.I (TAG, "dismiss");
if (Scanalertdialog!=null && scanalertdialog.isshowing ()) {
Scanalertdialog.dismiss ();
}
Default
Break
}

Related Article

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.