gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:42:48 25 4
gpt4 key购买 nike

我想从 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 ();
}

关于c# - 使用 Xamarin android 从 Asset 加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38504681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com