从monodroid类库中访问资产

问题描述 投票:2回答:4

是否可以从引用类库的monodroid应用程序中的monodroid类库中访问文件(使用构建操作“AndroidAsset”)?

我在类库中创建了一个“Assets”文件夹,并添加了一个带有构建操作“Android_Asset”的文本文件,但是从app中我无法通过Assets.Open(“file.txt”)访问它;

我希望来自类lib和主应用程序的资产可以某种方式“合并”......

xamarin.android assets
4个回答
1
投票

不 - 你不能从类lib中访问资产 - 只能从主exe项目中访问。

这类似于Java Android项目的情况。


围绕这个的一条路线可能是使用嵌入式资源 - 请参阅我在xamarin论坛上提出的这个问题的答案 - http://forums.xamarin.com/discussion/186/is-there-a-cross-platform-way-to-include-string-resources-from-class-library-projects - 请注意我还没有尝试过这个


0
投票

是否可以从引用类库的monodroid应用程序中的monodroid类库中访问文件(使用构建操作“AndroidAsset”)?

是。只需通过Context.Assets.Open“正常”访问它们。应用程序中只有一个资源来源,因此任何组件都可以访问任何和所有资产(如果他们有权访问AssetManager实例)。

但是,这只需要你面对的问题。图书馆项目可以使用Context.Assets属性吗?当然,如果你提供Context实例。

但是,图书馆项目是否可以提供自己的资产以包含在应用程序中?没有。

我在类库中创建了一个“Assets”文件夹,并添加了一个带有构建操作“Android_Asset”的文本文件,但是从app中我无法通过Assets.Open(“file.txt”)访问它;

库项目无法提供将包含在较大应用程序中的资产。 Java也不支持这个,afaik。


0
投票

要记住的事情

1构建操作是AndroidAsset(属性)

2复制到输出目录不要复制(属性)

3将文件添加到Assets文件夹

var destinationPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "yourfilename.txt");
            using (Stream source = Assets.Open("yourfilename.txt"))
            using (var dest = System.IO.File.Create (destinationPath)) {
                source.CopyTo (dest);
            }

有时这仍然会失败,monodroid bug。最好的办法是检查文件是否正确包含。

Load up Eclipse, in the device browser, you should see your simulator.  
Go to the directory data/app/    download the file yourappname.apk
This is just a zip file, so change the extension to .zip and open the
zip archive. And goto the folder /assets and see if the file you are trying 
to load is in there.  

此外,如果您尝试从物理设备上抓取文件进行测试,有些允许,有些则不允许。这在模拟器上测试最容易。


0
投票

可以从库中访问Assets文件:

var sourcePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//var files = Directory.GetFiles(sourcePath);
var filePath = Path.Combine(sourcePath, "MyAssetFile.bin");
var content = File.ReadAllBytes(filePath);
© www.soinside.com 2019 - 2024. All rights reserved.