文章目录
前言
访问原始文件
尽管并不常见,但您的确有可能需要访问原始文件和目录。如果确有需要,则将文件保存在 res/ 中并没有用,因为从 res/ 读取资源的唯一方法是使用资源 ID。您可以改为将资源保存在 assets/ 目录中。
保存在 assets/ 目录中的文件没有资源 ID,因此您无法通过 R 类或在 XML 资源中引用它们。您可以改为采用类似普通文件系统的方式查询 assets/ 目录中的文件,并利用 AssetManager 读取原始数据。
https://developer.android.google.cn/guide/topics/resources/providing-resources
一、将assets里面的json文件读到字符串里面
1. 将json文件放在assets里面,例如:papers.json
2. 读取方法
/**
* 读取json文件,将整个json文件作为字符串形式返回
* @param fileName json文件名(带后缀)
* @param context 上下文
* @return json文件内容的字符串形式
*/
public static String getJsonFile(String fileName, Context context) {
//将json数据变成字符串
StringBuilder stringBuilder = new StringBuilder();
try {
//获取assets资源管理器
AssetManager assetManager = context.getAssets();
//通过管理器打开文件并读取
BufferedReader bf = new BufferedReader(new InputStreamReader(
assetManager.open(fileName)));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
3. 调用
String response= ReadAssetsString.getJsonFile(“papers.json”,mContext);
二、将asset里面的文件复制到 /data/user/0/包名/files 中
1. 方法
/**
* 将asset里面的文件复制到
* /data/user/0/报名/files 中
* @param context
* @param assertName
*/
private void loadAssetsToCache(Context context, String assertName){
String filePath =this.getFilesDir().getAbsolutePath();
AssetManager assetManager = context.getAssets();
try {
InputStream inputStream=assetManager.open(assertName);
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
//保存到本地的文件夹下的文件
FileOutputStream fileOutputStream = new FileOutputStream(filePath + "/" + assertName);
byte[] buffer = new byte[1024];
int count = 0;
while ((count = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, count);
}
fileOutputStream.flush();
fileOutputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2. 调用
loadAssetsToCache(this,“china.vtpk”);
三、将asset里面的文件夹复制到 /data/user/0/包名/files 中
1. 方法
private void copyAssertDirToData(Context context, String assetDir, String dir) {
String[] files;
try {
files = context.getResources().getAssets().list(assetDir);
} catch (IOException e1) {
return;
}
File mWorkingPath = new File(dir);
if (!mWorkingPath.exists()) {
mWorkingPath.mkdirs();
}
for (int i = 0; i < files.length; i++) {
try {
String fileName = files[i];
if (!fileName.contains(".")) {
if (0 == assetDir.length()) {
copyAssertDirToData(context, fileName, dir + fileName + "/");
} else {
copyAssertDirToData(context, assetDir + "/" + fileName, dir +"/"+fileName);
}
continue;
}
File outFile = new File(mWorkingPath, fileName);
if (outFile.exists()) {
outFile.delete();
}
InputStream in = null;
if (0 != assetDir.length()){
in = context.getAssets().open(assetDir + "/" + fileName);
}else{
in = context.getAssets().open(fileName);
}
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 调用
/**
* RuntimeViewer3D assert中的文件夹
* tempPath 要复制到的文件夹的名称(全路径)
*/
String tempPath=context.getFilesDir().getAbsolutePath()+"/"+"RuntimeViewer3D";
copyAssertDirToData(this,"RuntimeViewer3D",tempPath);