android文件读取assets
来源:互联网 发布:网络科学导论讲义 编辑:程序博客网 时间:2024/04/29 05:26
assets文件夹资源的访问
assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。
1. 先在Activity里面调用getAssets() 来获取AssetManager引用。
2. 再用AssetManager的open(String fileName, int accessMode) 方法则指定读取的文件以及访问模式就能得到输入流InputStream。
3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close() 。
4.调用AssetManager.close() 关闭AssetManager。
需要注意的是,来自Resources和Assets 中的文件只可以读取而不能进行写的操作
以下为从Raw文件中读取:
代码
public String getFromRaw(){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
以下为直接从assets读取
代码
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
当然如果你要得到内存流的话也可以直接返回内存流!
try {
InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
以下为直接从assets读取
代码
public String getFromAssets(String fileName){
try {
InputStreamReader inputReader = new InputStreamReader( getResources().getAssets().open(fileName) );
BufferedReader bufReader = new BufferedReader(inputReader);
String line="";
String Result="";
while((line = bufReader.readLine()) != null)
Result += line;
return Result;
} catch (Exception e) {
e.printStackTrace();
}
}
当然如果你要得到内存流的话也可以直接返回内存流!
接下来,我们新建一个工程文件,命名为AssetsDemo。
然后建立一个布局文件,如下,很简单,无需我多介绍,大家一看就明白。
然后建立一个布局文件,如下,很简单,无需我多介绍,大家一看就明白。
然后呢,我从网上找了段文字,存放在assets文件目录下,取名为health.txt 这就是今天我们要读取的文件啦。
package com.assets.cn;
import java.io.InputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class AssetsDemoActivity extends Activity {
public static final String ENCODING = "UTF-8";
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(25.0f);
tv1.setText(getFromAssets("health.txt"));
}
//从assets 文件夹中获取文件并读取数据
public String getFromAssets(String fileName){
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
//获取文件的字节数
int lenght = in.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
import java.io.InputStream;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
public class AssetsDemoActivity extends Activity {
public static final String ENCODING = "UTF-8";
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv1 = (TextView)findViewById(R.id.tv1);
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(25.0f);
tv1.setText(getFromAssets("health.txt"));
}
//从assets 文件夹中获取文件并读取数据
public String getFromAssets(String fileName){
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
//获取文件的字节数
int lenght = in.available();
//创建byte数组
byte[] buffer = new byte[lenght];
//将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
这里是mainfest文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.assets.cn"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AssetsDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.assets.cn"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AssetsDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
0 0
- android assets 文件读取
- android文件读取assets
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- android 文件读取(assets)
- Android读取assets中的文件
- android 文件读取(assets)
- android读取assets文件示例
- android 文件读取(assets)
- ios自定义下拉列表
- 最长回文子串
- 初等几何(1):三角形、四边形、正多边形
- 2015 Xcode7 真机调试(开发者)以及P12配置---简书
- iOS UIcollectionview 数据量少时候bounces无效的解决方法
- android文件读取assets
- Rotate Image
- servlet知识点<四>
- nativejs调用安卓蓝牙
- Android读写assets目录下面的资源文件(文件夹)
- HDU 2817 A sequence of numbers [快速幂]
- 经济杠杆原理典型例子
- servlet知识点<五>
- 关于蓝桥杯STC15单片机的18B20程序