1。获取资源的输入流

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

Context.getAssets().open(“sample.txt”)

方法获取输入流。

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

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

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

WebView.loadUrl(“file:///android_asset/sample.html”);

加载html文件。

Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里。/res 和/assets的不同点是,android不为/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路径和文件名。下面这个例子,显示如何访问/assets下的内容。

在文件中/assets 中建立/image子目录,将/res/drawable下的icon.png子目录拷贝到该目录中。在/assets子目录中建立readme.txt文件,文件中输入文本“hello,world!!!”。

布局文件:main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<EditText android:id="@+id/firstId"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<EditText android:id="@+id/secondId"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

</LinearLayout>

程序文件:

package com.cn.getassets;

import android.app.Activity;

import android.os.Bundle;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import android.app.Activity;

import android.content.res.AssetManager;

import android.os.Bundle;

import android.util.Log;

import android.widget.EditText;

public class GetAssets extends Activity {

private EditText firstField;

private EditText secondField;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//  Log.d("show main.xml","ok");

setContentView(R.layout.main);

Log.d("show main.xml","ok");

AssetManager assetManager = getAssets();

String[] files = null;

try {

files = assetManager.list("image");

} catch (IOException e) {

Log.e("tag", e.getMessage());

}

firstField = (EditText) findViewById(R.id.firstId);

firstField.setText(Integer.toString(files.length)+"file.File name is"+ files[0]);

InputStream inputStream = null;

try {

inputStream = assetManager.open("readme.txt");

} catch (IOException e) {

Log.e("tag", e.getMessage());

}

String s = readTextFile(inputStream);

secondField = (EditText) findViewById(R.id.secondId);

secondField.setText(s);

}

private String readTextFile(InputStream inputStream) {

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

byte buf[] = new byte[1024];

int len;

try {

while ((len = inputStream.read(buf)) != -1) {

outputStream.write(buf, 0, len);

}

outputStream.close();

inputStream.close();

} catch (IOException e) {

}

return outputStream.toString();

}

}

Android读取assets目录下的资源的更多相关文章

  1. 安卓获取Assets目录下的资源

    获取Assets目录下的资源 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 ! ...

  2. [转]读取assets目录下的数据库文件

    在做Android应用的时候,不可避免要用到数据库.但是当我们把应用的apk部署到真机上的时候,已经创建好的数据库及其里边的数据是不能随着apk一起安装到真机上的. (PS:这篇博客解决了我前面博客中 ...

  3. Android 如何引用com.android.internal.R目录下的资源

    Android 如何引用com.android.internal.R目录下的资源 项目需求 有一个资源跟系统上的一个资源相同,想要引用它:frameworks/base/core/res/res/dr ...

  4. Android 读取assets文件下的txt文件

    android 读取assets文件下的txt文件,解决了读取txt文件的乱码问题: package com.example.com.scrollview; import java.io.Buffer ...

  5. Android从assets目录下读取文件相关

    有一个需求是app的帮助文档是word格式,ios可以直接用webview加载word显示,Android不行.而美工不配合转换成图片,前端没时间把word写成html 没办法,自己搞. 步骤: 1. ...

  6. Android复制assets目录下的图片到内存

    转自:http://www.chenwg.com/android/android%E5%A4%8D%E5%88%B6assets%E7%9B%AE%E5%BD%95%E4%B8%8B%E7%9A%84 ...

  7. android访问asset目录下的资源

    android提供了AssetManager来访问asset目录下的资源, 在activity中通过getAssets()获取AssetManager 常用的api如下: 1.列举路径下的资源Stri ...

  8. WebApp基础01-设置读取assets目录下文件

    要读取assets下的目录,只需要修改三个地方即可 1.res/layout/activity_main.xml 2.AndroidManifest.xml 3.src\com\example\lcy ...

  9. Android复制Assets目录下的文件到指定目录

    package com.android.demo; import java.io.File; import java.io.FileOutputStream; import java.io.Input ...

随机推荐

  1. 【转】发布的QT程序无法显示图标和图片的问题

    在windows下编译好的QT程序在其他没有安装QT的机器上会出现图标和图片无法正常显示的问题. 这时我们可以通过以下方式来解决: 在release文件夹里创建plugins文件夹,并将QT安装目录下 ...

  2. 一起学CUDA(一)

    前提是电脑的显卡支持CUDA,N卡一般是支持的,如果是A卡就没办法了.主要针对Windows环境,Linux和Mac也有相应的安装包.CUDA环境搭建:Step1:安装代码环境VS2010:Step2 ...

  3. DBA一天干的活

    一.检查活动状态 通过查询基本视图,确认数据库和实例处于正常运行状态,可以对外提供数据服务. 1.1实例状态 SELECT instance_name,status FROM v$instance; ...

  4. JS判断手机访问页面,根据手机访问或者PC访问跳转

    当用户访问你网站时,如果是PC端访问,则不作处理,如果是手机或者平板访问,就跳转到自己定义的手机页面去,这个在做webapp的时候经常用到,把代码分享给大家,希望对大家有所帮助. 首先,你要在页面中引 ...

  5. 职业操盘手内部教材 z

    重 点抢筹区:   是主力机构在低位拉高建仓后的一个相当尴尬的区域!因为在这个区域,场外的绝大多数投资者不敢买,而场内持有的人却很想卖!所以会出现成片的卖盘挂单! 由于主力向上做的意图已经非常明显,所 ...

  6. 记录一次cefsharp1输入法在win7下异常解决定位

    最近几天都被基于cefSharp封装的浏览器控件搞疯了!对于cefSharp基本满足当前所做项目的需求,但是有一个问题一直困扰我,那就是系统中偶尔会出现输入法不能转换到中文.而且这个问题似乎没有什么规 ...

  7. 【Linux】Semaphore信号量线程同步的例子

    0. 信号量 Linux下的信号量和windows下的信号量稍有不同. Windows Windows下的信号量有一个最大值和一个初始值,初始值和最大值可以不同.  而且Windows下的信号量是一个 ...

  8. SQL 教程学习进度备忘

    书签:跳过:另外跳过的内容有待跟进 __________________ 学习资源:W3School. _________________ 跳过的内容: 1.  “SQL select”底部的“ AD ...

  9. Page 63-64 Exercises 2.3.7 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)

    Use the following method printPrimes() for question a-d below //Find and prints n prime integers pri ...

  10. codejam环境熟悉—Minimum Scalar Product

    今天准备熟悉一下codejam的在线编程,为google的笔试做准备,因此按照codejam上对新手的建议,先用了一个简单的题目来弄清楚流程.记录一下需要注意的地方.   1.输入输出 输入输出重定位 ...