使用 Android 资产Using Android Assets
03/13/2018
本文内容
资产 提供了一种在应用程序中包含文本、xml、字体、音乐和视频等任意文件的方法。Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. 如果尝试将这些文件作为 "资源" 包含,则 Android 会将它们处理到其资源系统中,你将无法获取原始数据。If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data. 如果要访问无需访问的数据,则可以采用以下方法之一。If you want to access data untouched, Assets are one way to do it.
添加到你的项目的资产将与可使用 AssetManager从你的应用程序中读取的文件系统类似。Assets added to your project will show up just like a file system that can read from by your application using AssetManager.
在此简单演示中,我们将向项目中添加一个文本文件资产,使用将其读取, AssetManager 并在 TextView 中显示它。In this simple demo, we are going to add a text file asset to our project, read it using AssetManager, and display it in a TextView.
向项目添加资产Add Asset to Project
资产位于项目的 Assets 文件夹中。Assets go in the Assets folder of your project. 向此文件夹中添加名为的新文本文件 read_asset.txt 。Add a new text file to this folder called read_asset.txt. 在其中放置一些文本,如 "我来自资产!"。Place some text in it like "I came from an asset!".
Visual Studio 应将此文件的 生成操作 设置为 AndroidAsset:Visual Studio should have set the Build Action for this file to AndroidAsset:
Visual Studio for Mac 应将此文件的 生成操作 设置为 AndroidAsset:Visual Studio for Mac should have set the Build Action for this file to AndroidAsset:
选择正确的 BuildAction 可确保在编译时将该文件打包到 APK 中。Selecting the correct BuildAction ensures that the file will be packaged into the APK at compile time.
读取资产Reading Assets
Assets are read using an AssetManager. 的实例 AssetManager 可通过访问上的 资产 属性( Android.Content.Context 例如活动)来使用。An instance of the AssetManager is available by accessing the Assets property on an Android.Content.Context, such as an Activity.
在下面的代码中,我们将打开我们的 read_asset.txt 资产,读取内容并使用 TextView 将其显示。In the following code, we open our read_asset.txt asset, read the contents, and display it using a TextView.
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Create a new TextView and set it as our view
TextView tv = new TextView (this);
// Read the contents of our asset
string content;
AssetManager assets = this.Assets;
using (StreamReader sr = new StreamReader (assets.Open ("read_asset.txt")))
{
content = sr.ReadToEnd ();
}
// Set TextView.Text to our asset content
tv.Text = content;
SetContentView (tv);
}
读取二进制资产Reading Binary Assets
StreamReader在上述示例中使用的是文本资产的理想之选。The use of StreamReader in the above example is ideal for text assets. 对于二进制资产,请使用以下代码:For binary assets, use the following code:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Read the contents of our asset
const int maxReadSize = 256 * 1024;
byte[] content;
AssetManager assets = this.Assets;
using (BinaryReader br = new BinaryReader (assets.Open ("mydatabase.db")))
{
content = br.ReadBytes (maxReadSize);
}
// Do something with it...
}
运行应用程序Running the Application
运行应用程序,你应该会看到以下内容:Run the application and you should see the following:
相关链接Related Links