Android读取assets目录下的资源

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

}

}

时间: 2024-08-05 07:02:55

Android读取assets目录下的资源的相关文章

安卓获取Assets目录下的资源

获取Assets目录下的资源 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* BLOCKS =============================================================================*/ p, blockquote, ul, ol, dl, table, pre { margin: 15

Android开发系列(十七):读取assets目录下的数据库文件

在做Android应用的时候,不可避免要用到数据库.但是当我们把应用的apk部署到真机上的时候,已经创建好的数据库及其里边的数据是不能随着apk一起安装到真机上的. (PS:这篇博客解决了我前面博客中写的一个小游戏的一个问题,另外也可以读取Raw目录下的数据库文件) 这就造成了一个问题,这个问题其实很好解决,解决方法如下: 我们首先把有数据的数据库文件放在assets资源目录下边,然后在apk应用启动的时候,把assets目录下的数据库文件的数据写入到真机的内存中去. 下边开始我们的代码编写:

Android 如何引用com.android.internal.R目录下的资源

Android 如何引用com.android.internal.R目录下的资源 项目需求 有一个资源跟系统上的一个资源相同,想要引用它:frameworks/base/core/res/res/drawable/ic_text_dot.xml 文件名称:ic_text_dot.xml 文件的具体内容: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2014 The Androi

android访问asset目录下的资源

android提供了AssetManager来访问asset目录下的资源, 在activity中通过getAssets()获取AssetManager 常用的api如下: 1.列举路径下的资源String[] list(String path) 2.InputStream open(asset目录下的资源路径) 下面是放问asset目录下的图片的代码 package com.example.qunzheng.customerview; import android.app.Activity; i

unity3d 在android手机内读取assets目录内的资源

Unity3d版本(v4.6.3) android手机内读取包内文件只能使用www的方式, 文件协议为 "jar:file://" + Application.dataPath + "!/assets/" 1.必须有jar:开头 2.assets前有个!不能丢 3.目录路径分隔符不能有 \ 4.由于读取只能使用www的方式,故同时导致无法对assets内的文件进行遍历,若涉及到需要把资源拷贝到本地目录,则只能写一个记录文件,通常为一个文件名一行,如: aaa/bbb

Android复制Assets目录下的文件到指定目录

1 package com.android.demo; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.InputStream; 6 import android.content.Context; 7 public class CopyFileFromAssets { 8 /** 9 * 10 * @param myContext 11 * @param ASSETS_NAME 要复制的文件

WebApp基础01-设置读取assets目录下文件

1.res/layout/activity_main.xml加入代码,需要在xml布局文件中声明WebView组件 <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 2.在Activity中实例化WebView,并且可通过loadUrl(url)方法打开指定url资源

Android开发之assets目录下资源使用总结

预前知识: Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件比较简单,通过R.XXX.ID即可: 第二种是assets目录下存放的原生资源文件: 因为系统在编译的时候不会编译assets下的资源文件,所以我们不能通过R.XXX.ID的方式访问它们.那我么能不能通过该资源的绝对路径去访问它们呢?因为apk安装之后会放在/data/app/**.ap

Xamarin.Android 如何使用Assets目录下的文件

个人原创,转载注明出处:http://blog.csdn.net/supluo/article/details/43672411 Xamarin.Android  官网介绍地址:http://developer.xamarin.com/guides/android/application_fundamentals/resources_in_android/part_6_-_using_android_assets/ 这里插入一下Assets与Raw目录的异同点 这两个目录的相同点: 1. 这两个