/**
* 将asset文件写入缓存
*/private boolean copyAssetAndWrite(String fileName){ try {
File cacheDir=getCacheDir(); if (!cacheDir.exists()){
cacheDir.mkdirs();
}
File outFile =new File(cacheDir,fileName); if (!outFile.exists()){ boolean res=outFile.createNewFile(); if (!res){ return false;
}
}else { if (outFile.length()>10){//表示已经写入一次
return true;
}
}
InputStream is=getAssets().open(fileName);
FileOutputStream fos = new FileOutputStream(outFile); byte[] buffer = new byte[1024]; int byteCount; while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
is.close();
fos.close(); return true;
} catch (IOException e) {
e.printStackTrace();
} return false;
}
获取路径:
File dataFile=new File(getCacheDir(),fileName);Log.d(TAG,"filePath:" + dataFile.getAbsolutePath());