访问系统文件夹
Accessing system folders
准确地说,我正在尝试访问 Android、/Music
上的系统文件夹。我在文档中发现 getExternalFilesDir(Environment.DIRECTORY_MUSIC)
将授予对与我的应用程序相关的音乐文件夹的访问权限,这是它的一部分,一旦应用程序从设备中删除,最终将被删除。所以我的问题是如何访问系统音乐文件夹的内容?我是否拥有获取这些文件并稍后通过蓝牙发送的必要权限?
到目前为止我做了什么,但它显然列出了应用程序目录:
File path = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
Log.d("Files", "Path: " + path);
File f = new File(path,"");
File file[] = f.listFiles();
for (int i=0; i < file.length; i++)
{
filesList.setText("FileName:" + file[i].getName());
}
要获取设备上所有音乐的列表,您需要考虑使用 Content Providers and MediaStore
查看 Google's sample code 中的这段代码,了解如何从设备中检索音乐。它向您展示了具体操作方法。
重要的是这部分...
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// Perform a query on the content resolver. The URI we're passing specifies that we
// want to query for all audio media on external storage (e.g. SD card)
Cursor cur = mContentResolver.query(uri, null,
MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
if (cur == null) {
// Query failed...
return;
}
if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
return;
}
// retrieve the indices of the columns where the ID, title, etc. of the song are
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
准确地说,我正在尝试访问 Android、/Music
上的系统文件夹。我在文档中发现 getExternalFilesDir(Environment.DIRECTORY_MUSIC)
将授予对与我的应用程序相关的音乐文件夹的访问权限,这是它的一部分,一旦应用程序从设备中删除,最终将被删除。所以我的问题是如何访问系统音乐文件夹的内容?我是否拥有获取这些文件并稍后通过蓝牙发送的必要权限?
到目前为止我做了什么,但它显然列出了应用程序目录:
File path = getExternalFilesDir(Environment.DIRECTORY_MUSIC);
Log.d("Files", "Path: " + path);
File f = new File(path,"");
File file[] = f.listFiles();
for (int i=0; i < file.length; i++)
{
filesList.setText("FileName:" + file[i].getName());
}
要获取设备上所有音乐的列表,您需要考虑使用 Content Providers and MediaStore
查看 Google's sample code 中的这段代码,了解如何从设备中检索音乐。它向您展示了具体操作方法。
重要的是这部分...
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
// Perform a query on the content resolver. The URI we're passing specifies that we
// want to query for all audio media on external storage (e.g. SD card)
Cursor cur = mContentResolver.query(uri, null,
MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null);
if (cur == null) {
// Query failed...
return;
}
if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
return;
}
// retrieve the indices of the columns where the ID, title, etc. of the song are
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);