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

}

}

程序显示结果:使用模拟器。

最新文章

  1. 【python】with的实现方法
  2. 【转】VS2010安装后出现无法使用帮助的解决方案
  3. 数据字典生成工具之旅(7):NVelocity实现代码生成器
  4. 【BZOJ-4127】Abs 树链剖分 + 线段树 (有趣的姿势)
  5. 利用百度词典API和Volley网络库开发的android词典应用
  6. Android是如何绘制View的
  7. PLSQL_性能优化工具系列05_SQL Trace/Event 10046 Trace
  8. JavaScript标准Selection操作
  9. MIT 2012分布式课程基础源码解析-线程池实现
  10. GDI编程
  11. Python 字符、整型、列表字典等操作(二)
  12. 【中文分词】结构化感知器SP
  13. 弹出框插件layer使用
  14. Java 线程池(ThreadPoolExecutor)原理分析与使用
  15. 【SPOJ】DIVCNTK min_25筛
  16. 二级接口ListableBeanFactory
  17. Py中axis理解【转载】
  18. C#中winform使用相对路径读取文件的方法
  19. 43-将javaweb项目部署到Linux服务器
  20. singleInstance和singleTask导致startActivityForResult回调失败

热门文章

  1. 将Asp.Net MVC应用程序的控制器定义在单独的程序集(类库)中
  2. 移植linux4.7.2与ubifs到jz2440
  3. SVN--从本地检出项目至服务器报错--禁止访问
  4. eclipse svn 修改了类名之后提交
  5. uva 11076
  6. 一个很吊的swing循环生成窗口。
  7. 谁会是 Zabbix 和 Nagios 的继任者?
  8. .NET中数据集的强类型化
  9. gulp下livereload和webserver实现本地服务器下文件自动刷新
  10. **使用 Git Hook 实现网站的自动部署