且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的外部存储

更新时间:2023-11-22 21:48:52

正如我们在讨论的意见,我将展示的Andr​​oid音乐应用程序是如何找到的所有音乐文件。

As we discussed in comments, I'll show how Android Music application find all music files.

的Andr​​oid音乐应用程序只需要查询MediaProvider获得外部存储所有音乐,即SD卡。

Android Music application simply query the MediaProvider to get all musics on external storage, i.e. sdcard.

和数据库是由 MediaScannerService 。该MediaScannerService叫 MediaScanner.scanDirectories 搜索下那些目录中的所有文件。获取元数据如果是音频文件,并将其放入数据库(MediaProvider)。

And the databases is filled by MediaScannerService. The MediaScannerService call the MediaScanner.scanDirectories to search all files under those directories. Fetch metadata if it is audio file and put it into database(MediaProvider).

if (MediaProvider.INTERNAL_VOLUME.equals(volume)) {
    // scan internal media storage
    directories = new String[] {
        Environment.getRootDirectory() + "/media",
    };
}else if (MediaProvider.EXTERNAL_VOLUME.equals(volume)) {
    // scan external storage volumes
    directories = mExternalStoragePaths;
}

if (directories != null) {
    scan(directories, volume);
}

所以我的答案是MediaProvider已经包含在外部存储的音乐文件,这样你就可以直接查询供应商来获取所有的音乐文件。

So my answer is the MediaProvider already contains the music files on the external storage, so you can directly query the provider to get all music files.

MediaStore 第一。

您可以使用以下code让所有音乐文件。

You can use the following code to get all music files.

//Some audio may be explicitly marked as not being music
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

String[] projection = {
        MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.DATA,
        MediaStore.Audio.Media.DISPLAY_NAME,
        MediaStore.Audio.Media.DURATION
};

cursor = this.managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        null);

private List<String> songs = new ArrayList<String>();
    while(cursor.moveToNext()){
        songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" +   cursor.getString(2) + "||" +   cursor.getString(3) + "||" +  cursor.getString(4) + "||" +  cursor.getString(5));
}

MediaStore.Audio.Media.DATA 列包含完整路径音乐文件。

The MediaStore.Audio.Media.DATA column contains the full path to that music file.