assets目录下主要存放四种文件:文本文件、图像文件、网页文件(包括html中引用的js/ccs/jpg等资源)、音频视频文件。
转载自https://blog.youkuaiyun.com/kunga0814/article/details/80163208。
①加载assets目录下的网页
webView.loadUrl("file:///android_asset/html/index.htmll");
这种方式可以加载assets目录下的网页,并且与网页有关的css,js,图片等文件也会的加载。
②加载assets目录下的图片资源
InputStream is = getAssets().open(fileName);
bitmap = BitmapFactory.decodeStream(is);
ivImg.setImageBitmap(bitmap);
③加载assets目录下文本文件
InputStream is = getAssets().open(fileName);
int lenght = is.available();
byte[] buffer = new byte[lenght];
is.read(buffer);
String result = = new String(buffer, "utf8");
String str = null;
try {
InputStream is = getAssets().open("1.txt");
int lenght = is.available();
byte[] buffer = new byte[lenght];
is.read(buffer);
str = new String(buffer, "utf8").trim();
// int ss = str.split("\t").length;
if(str.equals("")){
}else{
text1.setText(str);
}
}catch(IOException e){
e.printStackTrace();
}
④加载assets目录下音乐
/ 打开指定音乐文件,获取assets目录下指定文件的AssetFileDescriptor对象
AssetFileDescriptor afd = am.openFd(music);
mPlayer.reset();
// 使用MediaPlayer加载指定的声音文件。
mPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(), afd.getLength());
// 准备声音
mPlayer.prepare();
// 播放
mPlayer.start()
⑤将assets下的文件复制到SD卡中
/**
* 从assets目录中复制整个文件夹内容
* @param context Context 使用CopyFiles类的Activity
* @param oldPath String 原文件路径 如:/aa
* @param newPath String 复制后路径 如:xx:/bb/cc
*/
public void copyFilesFassets(Context context,String oldPath,String newPath) {
try {
String fileNames[] = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名
if (fileNames.length > 0) {//如果是目录
File file = new File(newPath);
file.mkdirs();//如果文件夹不存在,则递归
for (String fileName : fileNames) {
copyFilesFassets(context,oldPath + "/" + fileName,newPath+"/"+fileName);
}
} else {//如果是文件
InputStream is = context.getAssets().open(oldPath);
FileOutputStream fos = new FileOutputStream(new File(newPath));
byte[] buffer = new byte[1024];
int byteCount=0;
while((byteCount=is.read(buffer))!=-1) {//循环从输入流读取 buffer字节
fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
}
fos.flush();//刷新缓冲区
is.close();
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//如果捕捉到错误则通知UI线程
MainActivity.handler.sendEmptyMessage(COPY_FALSE);
}
}
依次读取assets文件夹下子文件夹里的所有文件
db.beginTransaction(); // 开启事务,更快速
AssetManager am = getAssets();
String[] sf = null;
try {
// 获取assets文件夹下pureFeature子文件夹内的所有文件存入字符串数组
sf = am.list("pureFeature");
} catch (IOException e) {
e.printStackTrace();
}
String str = null, tmp = null;
for (int i = 0; i < sf.length; i++) {// 字符串数组索引从0开始,顺序读取内容
try {
tmp = "pureFeature/"+ sf[i]; // 将路径写全,sf[i]为对应的文件名
InputStream is = getAssets().open(tmp);
int length = is.available();
byte[] buffer = new byte[length];
is.read(buffer);
str = new String(buffer, "utf8").trim();
if (str.equals("")) {
} else {
text1.setText(str);
}
} catch (IOException e) {
e.printStackTrace();
}
String tp = sf[i].substring(0,sf[i].length()-4);//去掉每个文件的后缀“.txt”,
// subString(STATY,END)取的是[START,END)开区间
ContentValues values = new ContentValues();
values.put("data", str);
db.insert(Constant.TABLE_NAME, null, values);
Log.d(Constant.TAG, Integer.toString(i));
}
db.setTransactionSuccessful();//操作事务成功则返回
db.endTransaction();// 关闭数据库事务
db.close();