c# - 使用 Xamarin android 从 Asset 加载文件

我想从 Assets 中加载一个文件,我找到了解决方案,但使用的是 Java。如何将以下 Java 代码转换为 C#。

public String loadKMLFromAsset() {

    String kmlData = null;
    try {

        InputStream is = getAssets().open("yourKMLFile");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        kmlData = new String(buffer, "UTF-8");


    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return kmlData;

}

最佳答案

使用AssetManager

// 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 ();
}

https://stackoverflow.com/questions/38504681/

相关文章:

php - PHP 中的 fatal error 是什么意思?

php - 如何在 laravel 中返​​回一个响应对象 json

r - R中二维核密度估计的混淆

sql - 数据库中每个表的最大值(ID)

php - 如何取消设置特定用户的 session ?

c# - 如何在装饰器中定义事件和属性

ansible - 具有 SSH key 身份验证的用户如何在 Ansible 中拥有 sudo 权

java - 在不同计算机之间同步 IntelliJ 项目

scala - 为什么 For Comprehension 生成器会抑制 Option 类型?

cakephp - 如何从cakephp 3中的url访问参数