需求:curl需要支持https,需要手動設置ca地址用於驗證,對於安卓平台,需要將這個ca證書文件放到本地,然后獲取到絕對路徑
經過很多的嘗試,無論是放在res還是assets下,都沒能獲取到絕對路徑
解決方案:雖然不能獲取絕對路徑,但是能讀取到內容,就創建一個新的文件並拷貝內容,然后使用新建的文件路徑
/**
* android 獲取uri的正確文件路徑的辦法
* @param fileName assets下的文件名
* @return absolutePath copy文件到可寫目錄,並返回絕對路徑
*
* 有時會從其他的文件瀏覽器獲取路徑,這時根據路徑去數據庫取文件時會發現不成功,
* 原因是由於android的文件瀏覽器太多,各自返回的路徑不統一,
* 而android本身的數據庫中的路徑是絕對路徑,即"/mnt"開頭的路徑。
*/
private String 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 null;
}
} else {
if (outFile.length()>10){//表示已經寫入一次
return outFile.getPath();
}
}
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 outFile.getPath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
