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. centos在设置时区

    [root@localhost ~]# date -R     // 查看时区 Mon, 19 May 2014 10:18:46 +0000 [root@localhost ~]# tzselect ...

  2. C++面试宝典2011版

    1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new相应free仅仅会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,ne ...

  3. c++ primer 函数传值1

    不看c++ primer  永远不知道自己基础有多差 函数的參数传值一般有两种方式:值传递,引用传递. 值传递有以下两种形式: void func( int a ) { // } void func1 ...

  4. CAS Spring Security 3 整合配置(转)

    一般来说, Web 应用的安全性包括用户认证( Authentication )和用户授权( Authorization )两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否 ...

  5. 【课程分享】基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构、自己定义工作流)

    基于plusgantt的项目管理系统实战开发(Spring3+JDBC+RMI的架构.自己定义工作流) 课程讲师:张弘 课程分类:Java 适合人群:中级 课时数量:37课时 用到技术:Spring  ...

  6. akka.net与微软分布式框架Orleans

    微软分布式框架Orleans开源了 开源地址: https://github.com/dotnet/orleans 昨天编译了一下,这个最新的Orleans安装程序(用github源码编译的) 下载地 ...

  7. Android设计模式(十)--生成器模式

    回头看自己写的东西,大概Android当自己控制的定义,编写代码适用性比较高.但是,看看没有什么技术含量,因此,当在学习设计模式,想想有些东西是否可以改善,例如: 自己定义Dialog是Android ...

  8. Block学习一门:基本使用,使用block包NSURLRequest异步请求

    首先,看一下下面的代码: void (^myFirstBlock)(int theOne,int theTwo) = ^(int theOne,int theTwo){ NSLog(@"== ...

  9. php 无错误提示 的解决方法

    问:我在win7安装了PHP,浏览器是IE9.我代码写错了,浏览器一点错误提示都没有,一片空白.如果写对了,就能正常运行显示出来.请问这是怎么回事,应该怎么弄?你们两个的方法都试过,但都没有提示(注: ...

  10. Nancy 框架

    Nancy 框架 Nancy 框架 1.是一个轻量级用于构建http相应的web框架: 2.与mvc类似,有自己的路由机制: 3.可以处理 DELETE ,  GET ,  HEAD ,  OPTIO ...