AssetManager asset的使用

简介:

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;
 
importandroid.app.Activity ;
 
import android.content.res.AssetManager;
 
importandroid.os.Bundle ;
 
import android.util.Log;
 
import android.widget.EditText;
  
 
publicclass GetAssets extends Activity {
 
 private EditText firstField;
 
 private EditText secondField;
 
 @Override
 
 publicvoid 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();
 
 }
 
}


相关文章
whether logo retrieval will block the application
Created by Jerry Wang, last modified on Jun 17, 2015
whether logo retrieval will block the application
different way to retrieve SAP UI5 manifest
Created by Wang, Jerry, last modified on Jun 06, 2016
different way to retrieve SAP UI5 manifest
|
Web App开发 Java Android开发
Android中asset文件夹和raw文件夹区别
转载自:CSDN博客 这阵子有位同学在一个命名非“raw”的文件夹中放置mp3文件,导致R文件报错,故此研究,寻得此篇文章,特做分享。 摘自:http://www.cnblogs.com/leizhenzi/archive/2011/10/18/2216428.html Android中asset文件夹和raw文件夹区别 *res/raw和assets的相同点: 1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制。
1185 0
|
Java 开发工具 Android开发
Photo Sharing App Part II: Understanding OSS Functions & Creating UI
We will build a photo sharing Android app with real-time image uploading and downloading functionality using Alibaba Cloud OSS.
2038 0
|
Web App开发 PHP 图形学