Android中用Pdf.js 查看PDF文件,
docPath 為一個sdcard上的可訪問PDF文件路徑
loadUrl("file:///android_asset/web/viewer.html?file=" + docPath);
發(fā)現(xiàn)當path中存在# 時,文件不能打開,
首先想到對docPath做encode處理,以解決一些特殊字符問題;
大部分特殊字符encode后,都變得有效(不encode,文件打不開),
但發(fā)現(xiàn)當path中存在# 和 空格時,文件仍無法正常打開,當然,
如果不encode,空格是有效的;然后試著把空格replace回來,發(fā)現(xiàn)可以了;
但 “#” encode前后都不行,“#” encode后是“%23”,試著把“%”再encode一下,
變成“%2523”,就好了!!
if (!TextUtils.isEmpty(docPath)) {
String path = null;
try {// 獲取以字符編碼為utf-8的字符
path = URLEncoder.encode(docPath,"utf-8");
docPath = path.replace("%2F","/").replace("+", " ")
.replace("%23", "%2523");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
loadUrl("file:///android_asset/web/viewer.html?file=" + docPath);