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 ...
随机推荐
- ubuntu中KDE与GNOME安装切换
转载:http://apps.hi.baidu.com/share/detail/18919303 1.在Ubuntu中安装KDE桌面命令 sudo apt-get install kUbuntu-d ...
- Java二叉排序树(转)
一.二叉排序树定义 1.二叉排序树的定义 二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree).其定义为:二叉排序树或者是空树,或者是满足如下性 ...
- Android系统开发(2)——GDB调试工具
调试的过程 我们在eclipse中来看一下一般调试的过程: 1.debug模式编译 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iY ...
- Linux中查看socket状态(转)
Linux中查看socket状态:cat /proc/net/sockstat #(这个是ipv4的) sockets: used 137 TCP: inuse 49 orphan 0 tw 3272 ...
- PHPthinking官方论坛
PHPthinking官方论坛正式上线一个月!眼下.我们已经有数百个固定用户的.论坛发展迅速,所有份额一些技术贴,我们希望,其他许多用户增加来,创建学习.交流更方便.丰富的内容PHP座谈会! PHPt ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串
1 Encoding.Default.GetByteCount(checkString); =2 全角 =1 半角 /// <summary> /// 获取字符串的字节长度 /// &l ...
- 玩转Web之JavaScript(四)-----javaScript语法总结(四) JS中的函数
1.function/return function用来定义函数(位于head部分),函数包含着一些代码,这些代码只能被事件激活,或者在函数被调用时才会执行. return 用来从函数中返回值 ...
- wpf 9张图片的连连看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 一个非常有用的函数——COALESCE
原文:一个非常有用的函数--COALESCE 很多人知道ISNULL函数,但是很少人知道Coalesce函数,人们会无意中使用到Coalesce函数,并且发现它比ISNULL更加强大,其实到目前为止, ...