assets 加載資源文件

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

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



}java

相關文章
相關標籤/搜索