主题 : Android 如何读取assets目录下的资源 复制链接 | 浏览器收藏 | 打印
欢迎加入清源的android开发交流群:314230976,加群时请验证:arm,谢谢!
级别: 侠客
UID: 94332
精华: 0
发帖: 72
金钱: 370 两
威望: 74 点
贡献值: 0 点
综合积分: 144 分
注册时间: 2013-07-14
最后登录: 2013-09-25
楼主  发表于: 2013-09-17 17:17

 Android 如何读取assets目录下的资源

1. 获取资源的输入流

资源文件 test.txt 位于 $project_home/assets/ 目录下,可以在 Activity 中通过

复制代码
  1. (a)Context.getAssets().open(“test.txt”);
  2. (b)AssetManager assetManager = getAssets();
  3. InputStream is = assetManager.open("jin.txt");


方法获取输入流。

注意:如果资源文件是文本文件则需要考虑文件的编码和换行符。建议使用UTF-8。

2. WebView 加载assets目录下的html文件

资源文件 sample.html 位于 $PROJECT_HOME/assets/ 目录下,可以通过以下代码

复制代码
  1. WebView.loadUrl(“file:///android_asset/sample.html”);


加载html文件
欢迎加入android开发交流群,群号是:314230976