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
AssetManager asset使用的更多相关文章
- AssetManager asset的使用
Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里./res 和/assets的不同点是,android不为/assets下的文件生成ID.如果使用/a ...
- 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 ...
随机推荐
- 重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口
原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (26) ...
- Javascript中的__proto__、prototype、constructor
今天重温了下Javacript,给大家带来一篇Javascript博文,相信对于Javacript有一定了解的人都听过prototype原型这个概念,今天我们深度的分析下prototype与__pro ...
- Memcahce(MC)系列(两)Linux下一个Memcache安装
Linux下一个memcache安装 memcache是高性能.分布式的内存对象缓存系统,用于在动态应用中降低数据库负载.提升訪问速度.眼下用memcache解决互联网上的大用户读取是很流行的一种使用 ...
- 警报C++精密整数除法计算损失
非常偶然发现了一个精度损失的问题,简单来说: 有表达式: l = i/30 + j/40 + k/25, 求当{i,j,k} = {50,85,27}时l的值,非常easy,用计算器立即能够算出答案为 ...
- 文件搜索神器everything 你不知道的技巧总结
everything这个软件用了很久,总结了一些大家可能没注意到的技巧,分享给大家 1.指定文件目录搜索示例: TDDOWNLOAD\ abc 在所有TDDOWNLOAD文件夹下搜索包含 ...
- 【Android进阶】ZXing android 错误(Could not find class 'com.google.zxing.ResultPoint)
解决方法: 1.右键工程Build path, java build path,选择libraries 在右边的按钮中点击"Add Library" 选择"User li ...
- state-of-the-art implementations related to visual recognition and search
http://rogerioferis.com/VisualRecognitionAndSearch2014/Resources.html Source Code Non-exhaustive lis ...
- 面向对象的软件project——面向对象分析
为了解决软件危机.一些IT前辈国产软件project这个词汇,软件project它被引入到整个软件开发过程的维护. 软件project从程序的设计角度能够分为两类.一类是面向结构的软件project. ...
- mapxtreme演示V1.3
mapxtreme演示V1.3 mapxtreme地图相关基本功能的演示其中包括 鹰眼地图,图层控制,发达,缩小,平移地图,地图模糊查询,中点工具,距离测量工具,面积测量工具,图元信息查看工具,各 ...
- Web Host消息处理管道
Web Host消息处理管道 前言 我们知道Web API本身是无法提供请求-响应的机制,它是通过Web Host以及Self Host的寄宿的宿主方式来提供一个请求-响应的运行环境.二者都是将请求和 ...