Androidassets目录介绍和应用

说明:

1、文章转自:http://blog.toeach.net/2010/04/04/using-assets-in-android/

2、对原文的修改:增加注释,原英文已经翻译。

3、程序经过调试验证,运行正常。

4、原文题目:Using assets in Android

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"?>

<LinearLayoutxmlns: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 extendsActivity {

private EditTextfirstField;

private EditTextsecondField;

@Override

public voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Log.d("showmain.xml","ok");

setContentView(R.layout.main);

Log.d("showmain.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.Filename 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 StringreadTextFile(InputStream inputStream) {

ByteArrayOutputStreamoutputStream = new ByteArrayOutputStream();

byte buf[] = newbyte[1024];

int len;

try {

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

outputStream.write(buf, 0, len);

}

outputStream.close();

inputStream.close();

} catch (IOException e){

}

returnoutputStream.toString();

}

}

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

assets目录介绍和应用" height="462" width="650" style="border:1px solid black;">


转:http://blog.sina.com.cn/s/blog_6cf0d3f30100m2x6.html


更多相关文章

  1. Android(安卓)JB 4.2 中InputManager 分发键盘消息给应用程序的
  2. Android布局整合include界面控件
  3. android-实现黑名单拦截
  4. Android(安卓)选择文件并获取路径
  5. android_1
  6. Android(安卓)logback代码配置详解
  7. Android文件存储--采用SharedPreferences保存用户偏好设置参数和
  8. AndroidManifest.xml文件综合详解
  9. FFmpeg编程开发笔记 —— Android(安卓)FFmpeg + SDL2.0简易播放

随机推荐

  1. WebView 知识点
  2. Android开发之旅: Intents和Intent Filte
  3. 【Android基础】讲讲Android的事件分发机
  4. Android实现WebView删除缓存的方法
  5. android电话接听过程简单解析
  6. Android输入法弹出时覆盖输入框问题
  7. 浅谈Android下的Wifi&&安卓WIFI 移植
  8. Android(安卓)短信解析
  9. 【移动生活】Google项目副总裁安迪・鲁宾
  10. Android系列之广播