如何使用路径名从 windows phone (8.1) 音乐库中检索文件? [英] How to retrieve files from the windows phone (8.1) music library using path names?

查看:26
本文介绍了如何使用路径名从 windows phone (8.1) 音乐库中检索文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让我的用户从音乐库中选择文件.之后我将所选文件的路径存储在本地.现在我的问题是如何从这些路径以编程方式访问文件?以下是我尝试过的一些方法.

I am letting my users select files from the music library. After which I am storing the path of the selected files locally. Now my problem is how do I get access to the files programmatically from these paths? Here are a couple of things I've tried.

说我的路径名是 D:\Xyz\abc.mp3

StorageFolder S=KnownFolders.MusicLibrary;

备选方案 1:

StorageFile MyFile=await S.GetFileAsync("abc.mp3");//找不到文件错误

//我认为这不起作用,因为 GetFileAsync 方法只能从当前文件夹中获取文件

// I figure this doesn't work cause GetFileAsync method can only get files from the current folder

备选方案 2:

S=await S.GetFolderAsync("Xyz");//之后我打算做GetFileAsync.但这也会导致找不到文件夹的错误.

S=await S.GetFolderAsync("Xyz"); //After which I planned to do GetFileAsync. But this also gives a folder not found error.

最后我试过了 S=await S.GetFolderAsync("D");

任何有关我如何访问文件的帮助,甚至是实现我的目标的替代方法都很棒!

Any help as to how I get access to the file or even an alternate way to achieve my goal would be fantastic !

P.S : StorageFile.GetFileFromPathAsync("D:\Xyz\abc.mp3") 说我没有访问权限.

P.S : StorageFile.GetFileFromPathAsync("D:\Xyz\abc.mp3") says I don't have permission to access.

推荐答案

(答案重建更清晰)

如果你在 package.appxmanifest 文件中声明:

If you had declared in package.appxmanifest file:

  • Music Library 能力在 Capabilities
  • 文件类型关联声明中(如果您只访问音乐库,我不确定是否需要)
  • Music Library capability in Capabilities
  • File Type Association in Declarations (I'm not sure if this is needed if you only access Music Library)

然后您就可以像这样从 Music Library 文件夹中检索文件:

then you will be able to retrieve files from Music Library folder like this:

StorageFolder folder = KnownFolders.MusicLibrary;
// below code will work unless 'fileName.mp3' is in 
// your Music folder either on phone or SD card
StorageFile file = await folder.GetFileAsync("fileName.mp3");
// you can access them also with path, but in this case
// also Folders must be in Music folder
string path = @"Test\ccc.mp3";
StorageFile file = await folder.GetFileAsync(path);

总而言之,您只能通过 KnownFolders.MusicLibrary 访问位于手机或 SD 卡上但位于音乐文件夹中的文件 - 它们的路径将是:

To sum up you will be able to aceess by KnownFolders.MusicLibrary only to files which are either on Phone or SD card but in Music folder - their paths will be then:

C:\Data\Users\Public\Music\test.mp3 - 手机版

C:\Data\Users\Public\Music\test.mp3 - for Phone

D:\Music\Test\test2.mp3 - 用于 SD 卡

D:\Music\Test\test2.mp3 - for SD card

请记住通过 StorageFolder.GetFileAsync() 相对于 StorageFolder 工作(它也适用于完整路径,见下面的注释):

Remember that accessing by StorageFolder.GetFileAsync() works relative to the StorageFolder (it will also work with full path, see remark below):

要检索的文件的名称(或相对于当前文件夹的路径).

The name (or path relative to the current folder) of the file to retrieve.

在这种情况下,要获取上述文件,您将使用如下文件名:test.mp3Test\test2.mp3.

In this case to get above files you will use filenames like: test.mp3 and Test\test2.mp3.

将无法获得其他文件,例如 D:\xxx.mp3 - 它不在音乐库中 - 它在 SD 卡上,如果你想访问它你必须通过 KnownFolders.RemovableDevices - 当您添加了所需的功能时,您应该能够通过完整路径访问它.但也请记住这句话:

You won't be able to get other files like D:\xxx.mp3 - it isn't in Music Library - it is on SD card, if you want to access it you will have to do it by KnownFolders.RemovableDevices - when you have added required capabilities, then you should be able to access that via fullpath. But also please remember about the remark:

不要依赖此属性来访问文件,因为某些文件可能没有文件系统路径.例如,如果文件由 URI 支持,或者是使用文件选择器选择的,则不能保证该文件具有文件系统路径.

Do not rely on this property to access a file because some files may not have file-system paths. For example if the file is backed by a URI, or was picked using the file picker, the file is not guaranteed to have a file-system path.

在我们的讨论中发现您正在使用 CommonFileQuery.OrderByName 来获取文件.正如我在那里检查过的那样(IMO)可能是一个小错误 - 在 VS 描述中说:

In our discussion tured out that you are using CommonFileQuery.OrderByName to get files. As I've checked there (IMO) might be a little bug - in VS description says that:

深度查询的查询结果包括被查询文件夹的所有子文件夹中的所有文件,并根据指定的元数据对其进行排序.

Query results for deep queries include all files in all of the subfolders of the folder being queried and sorts them based on the specified metadata.

运行一段代码:

// a method to retrieve files from Musiic Library recursively 
private async Task RetriveFilesInFolders(List<StorageFile> list, StorageFolder parent)
{
   foreach (var item in await parent.GetFilesAsync()) list.Add(item);
   foreach (var item in await parent.GetFoldersAsync()) await RetriveFilesInFolders(list, item);
}

// then I've used it like this:

StorageFolder folder = KnownFolders.MusicLibrary;

List<StorageFile> listOfFiles = new List<StorageFile>();
await RetriveFilesInFolders(listOfFiles, folder);
// as a result of above code I have a List of 5 files that are in Music Library

List<IStorageItem> filesFolders = (await folder.GetItemsAsync()).ToList();
List<StorageFile> items = (await folder.GetFilesAsync(CommonFileQuery.OrderByName)).ToList();
// as a result here I've 14 files that are in Music Library and SD card, and Phone

已给出结果:

  • 在第一种情况下,5 个文件实际上位于 Music 文件夹及其子文件夹中,
  • 第二种情况 - 14 个文件位于:音乐库、SD 卡和手机

您可以在调试模式下轻松查看Path,您会看到第二种方法中的某些文件具有D:\file.mp3 等等 - 它们不在音乐库中 - 因此您将无法通过 KnownFolders.MusicLibrary 获取它们.如果您想获取它们,则必须使用其他已知文件夹 - 在本例中为 SD 卡:

You can easily look at the Path in debug mode and you will see that some of files in second method has D:\file.mp3 and so on - they aren't in Music Library - so you won't be able to get them by KnownFolders.MusicLibrary. If you want to get them then you will have to use other KnownFolders - in this case SD card:

StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;
StorageFolder sdCard = (await externalDevices.GetFoldersAsync()).FirstOrDefault();
StorageFile file = await sdCard.GetFileAsync("xyz.mp3"); // for D:\xyz.mp3
StorageFile file = await sdCard.GetFileAsync("XYZ\xyz.mp3"); // for D:\XYZ\xyz.mp3

这篇关于如何使用路径名从 windows phone (8.1) 音乐库中检索文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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