的Android不具有舊版本的支持。
試試這個工作對我來說..
webView = (WebView) view.findViewById(R.id.webView);
webView.setVisibility(View.VISIBLE);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) //required for running javascript on android 4.1 or later
{
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
}
settings.setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient());
webView.addJavascriptInterface(new MyJavascriptInterface(), "androidInterface");
加載PDF
private void loadPdfFile() {
String filePAth = getArguments().getString(PDF_FILE_PATH);
if (filePAth == null) {
return;
}
Uri path = Uri.parse(filePAth);
try {
Logger.info("PDF File Path " + path.toString());
InputStream ims = getResources().getAssets().open("pdfviewer/index.html");
String line = getStringFromInputStream(ims);
if (line.contains("THE_FILE")) {
line = line.replace("THE_FILE", path.toString());
FileOutputStream fileOutputStream = getActivity().openFileOutput("index.html", Context.MODE_PRIVATE);
fileOutputStream.write(line.getBytes());
}
} catch (IOException e) {
Logger.error("Errror on loading pdf file : " + path);
}
webView.loadUrl("file://" + getActivity().getFilesDir() + "/index.html");
mCurrentPage = 1;
}
//轉換的InputStream爲String
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
剛剛粘貼一段沒有任何解釋的代碼不是很有用,請解釋它的作用,以及爲什麼這個「是答案」 –