Android Assets(资产)
2018-02-18 16:35 更新
Android还提供了一个目录来存储资源文件:/assets
。
/assets
与 /res
的级别相同。
/assets
中的文件不会在 R.java
中生成ID。你必须指定读取它们的文件路径。
文件路径是以/assets
开头的相对路径。
例子
你可以使用 AssetManager
类来访问这些文件。
String getStringFromAssetFile(Activity activity) { AssetManager am = activity.getAssets(); InputStream is = am.open("test.txt"); String s = convertStreamToString(is); is.close(); return s; }
加载位于assets文件夹中的HTML文件
以下代码显示如何加载位于assets文件夹中的HTML文件。
主布局xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <WebView android:id="@+id/webview1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
在MainActivity.java文件中,添加以下语句:
import android.app.Activity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; /*from www.w3cschool.cn*/ public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); WebView wv = (WebView) findViewById(R.id.webview1); wv.loadUrl("file:///android_asset/Index.html"); } }
从asset文件夹读取并解析CSV文件
下面的代码显示了如何从asset文件夹读取并解析CSV文件。
注册 WRITE_EXTERNAL_STORAGE
的权限
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.w3cschool.myapplication3.app" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="www.w3cschool.cn" android:theme="@style/AppTheme" > <activity android:name="cn.w3cschool.myapplication3.app.MainActivity" android:label="www.w3cschool.cn" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
主Activity的Java代码
package cn.w3cschool.myapplication3.app; //from www.w3cschool.cn import android.app.Activity; import android.content.res.AssetManager; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); setContentView(tv); try { AssetManager manager = getAssets(); InputStream in = manager.open("data.csv"); ArrayList<Person> cooked = parse(in); StringBuilder builder = new StringBuilder(); for(Person piece : cooked) { builder.append(String.format("%s is %s years old, and likes the color %s", piece.name, piece.age, piece.color)); builder.append("\n"); } tv.setText(builder.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /* Simple CSV Parser */ private static final int COL_NAME = 0; private static final int COL_AGE = 1; private static final int COL_COLOR = 2; private ArrayList<Person> parse(InputStream in) throws IOException { ArrayList<Person> results = new ArrayList<Person>(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String nextLine = null; while ((nextLine = reader.readLine()) != null) { String[] tokens = nextLine.split(","); if (tokens.length != 3) { Log.w("CSVParser", "Skipping Bad CSV Row"); continue; } //Add new parsed result Person current = new Person(); current.name = tokens[COL_NAME]; current.color = tokens[COL_COLOR]; current.age = tokens[COL_AGE]; results.add(current); } in.close(); return results; } } class Person { public String name; public String age; public String color; public Person() { } }
data.csv
Jack,Red,12 Jane,Black,13 Jode,Brown,14
以上内容是否对您有帮助:
更多建议: