使用Xamarin android从Asset加载文件

dou*_*dou 2 c# assets xamarin.android

我想从Asset加载文件,我找到了解决方案,但是使用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;

}
Run Code Online (Sandbox Code Playgroud)

mmu*_*taq 6

使用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 ();
}
Run Code Online (Sandbox Code Playgroud)