Load a file from Asset using Xamarin android without AssetManager (need path to file)

huangapple go评论63阅读模式
英文:

Load a file from Asset using Xamarin android without AssetManager (need path to file)

问题

I am using a piece of code, that requires a path to a file to function. How can I get the raw path to the data on android? (Said data is currently in the Assets folder, but I can move it somewhere else.) Are there any workarounds?

我正在使用一段代码,需要文件路径才能正常运行。如何在Android上获取数据的原始路径?(所谓的数据目前位于Assets文件夹中,但我可以将其移动到其他地方。)是否有任何解决方法?

void FunnyCode(string filePath);

FunnyCode(/???/);

英文:

I am using a piece of code, that requires a path to a file to function. How can I get the raw path to the data on android? (Said data is currently in the Assets folder, but I can move it somewhere else.) Are there any workarounds?

void FunnyCode(string filePath);

FunnyCode(/*???*/);

答案1

得分: 1

根据这个关于如何从资产文件获取URI的案例,没有“资产文件夹中文件的绝对路径”。项目资产的内容打包在APK文件中。

而且资源文件夹也应该与资产文件夹相同。所以您可以尝试使用AssetManager将文件复制到设备上的本地文件夹。您可以参考这个关于Xamarin.Android中如何将Asset文件夹内容复制到内部存储的案例

此外,如果您只想读取文件,您可以使用AssetManager.List()来获取资产文件夹中的所有文件,然后使用AssetManager.Open()来读取文件。

英文:

According to this case about How to get URI from an asset File, there is no "absolute path for a file existing in the asset folder". The content of your project's assets are packaged in the APK file.

And the resource folder should also be same as the asset folder. So you can try to use the AssetManager to copy the file to a local folder on the device. You can refer to this case about How to copy Asset folder content into internal storage in Xamarin.Android?

In addition, if you just want read the file. You can use the AssetManager.List() to get all the file in the Asset folder and AssetManager.Open() to read the file.

huangapple
  • 本文由 发表于 2023年4月17日 01:41:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定