Android读取assets目录下的资源_第1页
Android读取assets目录下的资源_第2页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Android读取assets目录下的资源1。获取资源的输入流资源文件sample.txt位于$PROJECT_HOME/assets/目录下,可以在Activity中通过Context.getAssets().open(“sample.txt")方法获取输入流。注意:如果资源文件是文本文件则需要考虑文件的编码和换行符。建议使用UTF-8和Unix换行符。WebView加载assets目录下的html文件资源文件sample.html位于$PROJECT_HOME/assets/目录下,可以通过以下代码WebView.loadUrl(“HYPERLINKfile:///android_asset/sample.html")加载html文件。Android系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里。/res和/assets的不同点是,android不为/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路径和文件名。下面这个例子,显示如何访问/assets下的内容。在文件中/assets中建立/image子目录,将/res/drawable下的icon.png子目录拷贝到该目录中。在/assets子目录中建立readme.txt文件,文件中输入文本“hello,world!!!”。布局文件:main.xmlv?xmlversion="1.0"encoding="utf-8"?>vLinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">vTextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/>vEditTextandroid:id="@+id/firstId"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/>vEditTextandroid:id="@+id/secondId"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/></LinearLayout>程序文件:.getassets;importandroid.app.Activity;importandroid.os.Bundle;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.InputStream;importandroid.app.Activity;importandroid.content.res.AssetManager;importandroid.os.Bundle;importandroid.util.Log;importandroid.widget.EditText;publicclassGetAssetsextendsActivity{privateEditTextfirstField;privateEditTextsecondField;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);//Log.d("showmain.xml","ok");setContentView(R.layout.main);Log.d("showmain.xml","ok");AssetManagerassetManager=getAssets();String]]files=null;try{files=assetManager.list("image");}catch(IOExceptione){Log.e("tag",e.getMessage());}firstField=(EditText)findViewById(R.id.firstId);firstField.setText(Integer.toString(files.length)+"file.Filenameis"+files[0]);InputStreaminputStream=null;try{inputStream=assetManager.open("readme.txt");}catch(IOExceptione){Log.e("tag",e.getMessage());}Strings=readTextFile(inputStream);secondField=(EditText)findViewById(R.id.secondId);secondField.setText(s);}privateStringreadTextFile(InputStreaminputStream){ByteArrayOutputStreamoutputStream=newByteArrayOutputStream();bytebuf[]=newbyte[1024];intlen;try{while((len=inputStream.read(buf))!=-1){outputStre

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论