AssetManager asset的使用
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();
}
}
程序显示结果:使用模拟器。
AssetManager asset的使用的更多相关文章
- AssetManager asset使用
Android 该系统提供了一个程序为每个新的设计/assets文件夹.保存该文件在此文件夹可以在一个程序被打包./res 和/assets所不同的是,android不/assets下生成的文件ID. ...
- AssetManager
AssetManager用于获取assets下的资源. 1.getassets()得到AssetManager 2.AssetManager.close() 关闭AssetManager 3.Reso ...
- Android使用pull解析xml
一.理论准备 Pull解析器的运行方式与 SAX 解析器相似.它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件.跟SAX不同的是, ...
- Android的Bitmap和BitmapDrawable类解析-android学习之旅(六十)
使用简单图片 使用Drawable对象 bitmap和BitmapDrawable对象 package peng.liu.test; import android.app.Activity; impo ...
- OpenGL—Android 开机动画源码分析一
.1 Android开机动画实现方式目前实现Android开机动画的方式主要是逐帧动画和OpenGL动画. ?逐帧动画 逐帧动画是一种常见的动画形式(Frame By Frame),其原理是在“连续的 ...
- ListView下拉刷新、上拉载入更多之封装改进
在Android中ListView下拉刷新.上拉载入更多示例一文中,Maxwin兄给出的控件比较强大,前面有详细介绍,但是有个不足就是,里面使用了一些资源文件,包括图片,String,layout,这 ...
- XML解析之SAX
今天在敲代码的时候,想要实现地址选择功能,就是那个能够选择省.市.县的一个,用到的一个开源框架Android-PickerView,当然他这个里面尽管实现了能够选择的城市列表.可是他这是自己创建的,可 ...
- android仿iphone的地区选择
最近项目要做一个,类似淘宝手机客户端的,选择收货地址的三级联动滚动选择组件,下面是它的大致界面截图: 在IOS中有个叫UIPickerView的选择器,并且在dataSource中定义了UIPicke ...
- Android的原始资源Raw和Assert资源的使用-android学习之旅(五十七)
代码示例 public class MainActivity extends Activity{ MediaPlayer mediaPlayer1,mediaPlayer2; @Override pr ...
随机推荐
- 原型prototype -- 深入理解javascript
/* 原型Prototype */ //一.原型 //原型使用一 var calculator = function (dlg, tax) { this.dlg = dlg; this.tax = t ...
- 怎么让CentOS集群自动同步时间
怎么让CentOS集群自动同步时间?首先机器要连外网,这样才能从互联网上同步时间,这是首先要了解的.好了,主要的方法如下: 在除了运行ntpd之外的机器上,执行: [html] # chkconfig ...
- PHP读取xml方法讲解
一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Marku ...
- 层次数据结构字符串处理,split函数使用
String str1 = "11@22#33,44,55,#bb#cc,dd,ee,#@DDD@TTT#999,#@"; String[] CX = str1.split(&qu ...
- HDOJ 1284 钱币兑换问题
转自:wutianqi http://www.wutianqi.com/?p=981 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1284 tag:母 ...
- django --fields.E304 错误解决方案
今天在同一个表里,有多个不同的用户集时出现. fields.E304: Field name <field name> clashes with accessor for <fiel ...
- 关于DotNetBar中DataGridViewX 自动全屏 Anchor属性无效问题
由于在DataGridViewX 中使用了控件DataGridViewCheckBoxXColumn会导致 Anchor属性无效问题化,具体原因未知,建议改换为系统自带的DataGridViewChe ...
- Android 设置界面的圆角选项
请先看一个图: 像这种界面的设计,其实是这样的:整体为一个LinearLayout,一个一个排下去,每一个点击项都是一个RelativeLayout(要为其设置clickable为true),分为左右 ...
- P1005 采药
P1005 采药 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2005复赛普及组第三题 描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的 ...
- kmeans理解
最近看到Andrew Ng的一篇论文,文中用到了Kmeans和DL结合的思想,突然发现自己对ML最基本的聚类算法都不清楚,于是着重的看了下Kmeans,并在网上找了程序跑了下. kmeans是unsu ...