Android的瀏覽器沒有內置的PDF支持默認所以這個不能輕易與實現。
但是這裏有兩件事你可以做。
1)下載,並從Play商店安裝Adobe Reader,然後嘗試下面的代碼
首先你在設備內存中創建一個文件,然後你閱讀PDF數據進去。 「theurl」是其中的PDF位於
InputStream in = null;
File dst = new File(Environment.getExternalStorageDirectory()+"/myPDF.pdf");
try {
in = theurl.openStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStream out = null;
try {
out = new FileOutputStream(dst);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
}
// Transfer bytes from in to out
try {
byte[] buf = new byte[100000];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent();
Uri path = Uri.fromFile(dst);
intent.setDataAndType(path, "application/pdf");
startActivity(intent);
這將下載的PDF格式,請在設備內存中創建它,然後意圖將使用adove讀者您的設備上打開它通過應用程序的URL。
另一種選擇是
2)進入http://jaspreetchahal.org/android-how-to-open-pdf-file-within-the-browser/,看到那傢伙是怎麼做的
請回應下面的答案,說它是否對你有幫助 – krilovich