AssetManager asset的使用
大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。
Android 系统为每一个新设计的程序提供了/assets文件夹,这个文件夹保存的文件能够打包在程序里。/res 和/assets的不同点是,android不为/assets下的文件生成ID。假设使用/assets下的文件,须要指定文件的路径和文件名称。以下这个样例,显示怎样訪问/assets下的内容。
在文件里/assets 中建立/image子文件夹,将/res/drawable下的icon.png子文件夹复制到该文件夹中。在/assets子文件夹中建立readme.txt文件,文件里输入文本“hello,world!!!”。
<?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();
}
}
程序显示结果:使用模拟器。
http://blog.sina.com.cn/s/blog_6cf0d3f30100m2x6.html
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/118595.html原文链接:https://javaforall.cn
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击