引用:http://abc20899.iteye.com/blog/1096620

1。获取资源的输入流

资源文件 sample.txt 位于 $PROJECT_HOME/assets/ 目录下,可以在 Activity 中通过

Context.getAssets().open(“sample.txt”)

方法获取输入流。

注意:如果资源文件是文本文件则需要考虑文件的编码和换行符。建议使用UTF-8和Unix换行符。

2. WebView 加载assets目录下的html文件

资源文件 sample.html 位于 $PROJECT_HOME/assets/ 目录下,可以通过以下代码

WebView.loadUrl(“file:///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.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();

}

}

assets 加载资源文件的更多相关文章

  1. Maven,预加载资源文件

    预加载资源文件需要先启用功能: <build> <resources> <resource> <directory>src/main/resources ...

  2. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  3. Java加载资源文件的两种方法

    处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...

  4. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

  5. Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)

    Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1.       内联样式 ...

  6. PyQt5(5)——加载资源文件

    在实际中我们需要美化界面,就需要许多的自定义图片. 但是我们发现直接导入图像使用,等程序运行时会报错.???? 这就需要建立资源文件并且加载它们,程序就可以顺利运行了. 设计界面是如何加载资源文件呢? ...

  7. Java加载资源文件几种方法

    from: http://andyzhu.blog.51cto.com/4386758/775836/ import java.net.URL; import org.springframework. ...

  8. NHibernate动态加载资源文件

    最近做项目,又用到了以前做过的ORM框架--NHibernate. 此次想要实现的目标: 1.简单SQL用NHibernate的Session的CRUD方法实现 2.复杂SQL用Native SQL实 ...

  9. java加载资源文件

    className.class.getResourceAsStream 用法: 第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类Test.class ,同时有资源文件c ...

随机推荐

  1. PHP 学习笔记---基本语法

    ------php语言与JavaScript的使用 方法是相似 <script type="text/javascript"> </script>--js与 ...

  2. php调试局部错误强制输出 display_errors

    error_reporting(E_ALL); ini_set('display_errors', '1'); ini_set('error_log', dirname(__FILE__) . '/e ...

  3. java命令行引入jar包

    编译: E:/>javac -cp e:/jdom.jar test1.java 执行: E:/>java -classpath e:/jdom.jar; test1

  4. 【iCore3应用开发平台】发布 iCore3 应用开发平台PID控制代码

    说明:1.本代码包包含FPGA和STM32F407两部分内容2.FPGA工程为出厂代码FPGA工程,版本为REV43.STM32F407为只含PID控制的ARM工程4.在使用风扇过程中,请勿将手伸入扇 ...

  5. PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密

    <?php /* 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密 */ // 加密 $algorithm = MCRY ...

  6. true_kb

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. Secure Digital

    https://en.wikipedia.org/wiki/Secure_Digital#Technical_details Secure Digital (SD) is a non-volatile ...

  8. 数据结构与算法分析——C语言描述 第三章的单链表

    数据结构与算法分析--C语言描述 第三章的单链表 很基础的东西.走一遍流程.有人说学编程最简单最笨的方法就是把书上的代码敲一遍.这个我是头文件是照抄的..c源文件自己实现. list.h typede ...

  9. Virtualbox 虚拟机支持硬件摄像头

    最近我们公司做了一个摄像头项目,需要测试各种浏览器的情况,我就安装了一个Win xp的虚拟机,但是发现无法找到摄像头,经过查阅资料找到了解决办法 前提环境 Mac电脑 Virtualbox 虚拟机 虚 ...

  10. Spark会把数据都载入到内存么

    转载自:https://www.iteblog.com/archives/1648 前言: 很多初学者其实对于Spark的编程模式还是RDD这个概念理解不到位,就会产生一些误解.比如,很多时候我们常常 ...