直接使用路径不管用 所以先把文件拷贝到手机内存 再获取文件的路径
/**
* 将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;
}
/**
* 获取已经存入的文件的路径
*
* @param fileName
* @return
*/
private String getFilePath(String fileName) {
File dataFile = new File(getCacheDir(), fileName);
return dataFile.getPath();
}
记下来 方便copy