Android的外部存储 [英] Android external storage

查看:164
本文介绍了Android的外部存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的方法 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)来获取默认的目录Android设备保存其音乐。

I'm using method Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) to get the default directory my Android device saves its music to.

通过此方法返回的目录是否存在(像SD卡/音乐),但它不具有任何音乐文件。当我检查我的SD卡,我发现所有的音乐文件,而不是存储在我的应用程序(SD卡/ qqmusic /歌曲),它可以不使用被发现了一个名为另一目录 getExternalStoragePublicDirectory 方法。

The directory returned by this method exists(something like sdcard/music), but it doesn't have any music files. When I checked my sdcard I found all the music files are instead stored in another directory named by one of my applications(sdcard/qqmusic/song), which cannot be found using the getExternalStoragePublicDirectory method.

在这种情况下(不同的设备上存储音乐在不同的地点),有没有什么好的办法设备中获取的路径,无论有什么区别?

In such case(different devices store music in different locations), is there any good ways to get the paths regardless of the differences among devices?

推荐答案

正如我们在讨论的意见,我将展示的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.

和数据库是由<填充href=\"https://android.googlesource.com/platform/packages/providers/MediaProvider/+/android-4.2.2_r1/src/com/android/providers/media/MediaScannerService.java\"相对=nofollow> MediaScannerService 。该MediaScannerService叫<一个href=\"https://android.googlesource.com/platform/frameworks/base/+/android-4.2.2_r1/media/java/android/media/MediaScanner.java\"相对=nofollow> 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.

这篇关于Android的外部存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆