Android assets 目录介绍和应用
2014-03-02 10:37
399 查看
说明:
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"?>
<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、文章转自: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"?>
<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 目录介绍和应用
- Android assets 目录介绍和应用
- Android应用开发工程目录作用介绍
- Android应用相关文件目录、清除数据和缓存的介绍
- Android中读取assets目录下的文件详细介绍
- Android中读取assets目录下的文件详细介绍
- android应用Assets目录下的资源
- Android Map新用法:MapFragment应用介绍
- Android应用Design Support Library介绍
- Android中对assets目录下数据库操作
- android 使用MediaPlayer播放assets目录的音频文件
- Android之assets资源目录的各种操作
- Android应用的基本组件介绍
- Android开发环境与文件目录介绍
- Eclipse中新建立android工程目录和文件作用介绍
- Android 工程目录结构介绍
- 第2.3节 android目录中manifest的介绍
- Android主流HOOK框架介绍与应用--游戏破解游戏外挂的必杀技
- android 应用的基本组件介绍