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();

}

}

该方案显示结果:使用模拟器。

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

AssetManager asset使用的更多相关文章

  1. AssetManager asset的使用

    Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里./res 和/assets的不同点是,android不为/assets下的文件生成ID.如果使用/a ...

  2. AssetManager

    AssetManager用于获取assets下的资源. 1.getassets()得到AssetManager 2.AssetManager.close() 关闭AssetManager 3.Reso ...

  3. Android使用pull解析xml

    一.理论准备     Pull解析器的运行方式与 SAX 解析器相似.它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件.跟SAX不同的是, ...

  4. Android的Bitmap和BitmapDrawable类解析-android学习之旅(六十)

    使用简单图片 使用Drawable对象 bitmap和BitmapDrawable对象 package peng.liu.test; import android.app.Activity; impo ...

  5. OpenGL—Android 开机动画源码分析一

    .1 Android开机动画实现方式目前实现Android开机动画的方式主要是逐帧动画和OpenGL动画. ?逐帧动画 逐帧动画是一种常见的动画形式(Frame By Frame),其原理是在“连续的 ...

  6. ListView下拉刷新、上拉载入更多之封装改进

    在Android中ListView下拉刷新.上拉载入更多示例一文中,Maxwin兄给出的控件比较强大,前面有详细介绍,但是有个不足就是,里面使用了一些资源文件,包括图片,String,layout,这 ...

  7. XML解析之SAX

    今天在敲代码的时候,想要实现地址选择功能,就是那个能够选择省.市.县的一个,用到的一个开源框架Android-PickerView,当然他这个里面尽管实现了能够选择的城市列表.可是他这是自己创建的,可 ...

  8. android仿iphone的地区选择

    最近项目要做一个,类似淘宝手机客户端的,选择收货地址的三级联动滚动选择组件,下面是它的大致界面截图: 在IOS中有个叫UIPickerView的选择器,并且在dataSource中定义了UIPicke ...

  9. Android的原始资源Raw和Assert资源的使用-android学习之旅(五十七)

    代码示例 public class MainActivity extends Activity{ MediaPlayer mediaPlayer1,mediaPlayer2; @Override pr ...

随机推荐

  1. Silverlight之 xaml布局

    目标:在两周内完成一个界面的功能 第1阶段:完成xaml的布局 准备:视频4-14节 第2阶段: 完成环状图 柱状图 TreeView样式 准备: 矢量绘图  telerik 自定义控件  自定义控件 ...

  2. Windows Phone 8 - Runtime Location API - 2

    原文:Windows Phone 8 - Runtime Location API - 2 在<Windows Phone 8 - Runtime Location API - 1>介绍基 ...

  3. java编程中容易犯2的细节汇总

    1.for() 和 while() 执行步骤 for(s1;s2;s3){ s4;} 1.进入循环执行s1;       2.执行s2;//条件为真才执行s4,不然就跳出for了.      3,执行 ...

  4. Windows Phone 8.1 新功能 - 应用栏控件

    2014年4月3日的微软Build 2014 大会上.Windows Phone 8.1 正式公布. 相较于Windows Phone 8.不论从用户还是开发人员的角度,都产生了非常大的变化. 接下来 ...

  5. 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)

    原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...

  6. UVALive 5103 Computer Virus on Planet Pandora Description 一些新兴需求模式的字符串 AC自己主动机

    主题链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=3104">点击打开链接 题意: ...

  7. 让你提前认识软件开发(17):makefile文件的书写及应用

    第1部分 又一次认识C语言 makefile文件的书写及应用 [文章摘要] makefile用于Linux下整个project的编译.对于Linux下的C/C++语言的编译是至关重要的. 本文以实际的 ...

  8. MVC Code First (代码优先)

    首先配置web.config <connectionStrings> <add name="BookDbContext" connectionString=&qu ...

  9. Spark里边:Worker源代码分析和架构

    首先由Spark图表理解Worker于Spark中的作用和地位: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW56aHNvZnQ=/font/5a6L ...

  10. 允许debian wheezy支持IOS7+的iphone.

    IOS更新, 连接到数据线,不能使用 我想复制iphone照片只能用于内itunes对?  于linux这里面其实很容易处理. 在这里,我们使用了一个相对较新的组件libimobiledevice 为 ...