https://col1.3yd.co:8087/api/getDoc?用户名=test&密码=12345678&doc_id=dc_1
问题是当我使用在线谷歌文档查看器在 webview 中打开此链接时,我收到错误“哎呀!!显示此图像时出现问题。”
https://docs.google.com/gview?embedded=true&url=https://col1.3yd.co:8087/api/getDoc?username=test&password=12345678&doc_id=dc_1
在尝试了 stackoverflower 的太多方法后,我使用此代码加载 pdf 文件。 问题是每次下载文件都需要时间。有没有其他方法可以在 webview 中显示文件。
private void downloadAndOpenPDF(){
progressDialog = new DLProgressDialog(context);
progressDialog.show();
new Thread(new Runnable() {
public void run() {
Uri path = Uri.fromFile(downloadFile(download_file_url));
try {
if(progressDialog.isShowing()) {
progressDialog.dismiss();
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
}).start();
}
private File downloadFile(String dwnload_file_path) {
File file = null;
try {
URL url = new URL(dwnload_file_path);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// connect
urlConnection.connect();
// set the path where we want to save the file
File SDCardRoot = Environment.getExternalStorageDirectory();
// create a new file, to save the downloaded file
file = new File(SDCardRoot, dest_file_path);
FileOutputStream fileOutput = new FileOutputStream(file);
// Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
// this is the total size of the file which we are
// downloading
totalsize = urlConnection.getContentLength();
// create a buffer...
byte[] buffer = new byte[1024 * 1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
per = ((float) downloadedSize / totalsize) * 100;
}
// close the output stream when complete //
fileOutput.close();
} catch (final MalformedURLException e) {
} catch (final IOException e) {
} catch (final Exception e) {
}
return file;
}
在 Android 中使用 Barteksc 从互联网加载 PDF
如何从互联网在您的 Android 应用程序中显示 pdf 显示。非常简单的步骤来做到这一点
private class RetrivePDFfromUrl extends AsyncTask<String, Void, InputStream> {
@Override
protected InputStream doInBackground(String... strings) {
InputStream inputStream = null;
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
if (urlConnection.getResponseCode() == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
pdfView.fromStream(inputStream)
.enableSwipe(true)
.swipeHorizontal(true)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
.enableAntialiasing(true)
.spacing(0)
.pageFitPolicy(FitPolicy.WIDTH)
.pageSnap(true) // snap pages to screen boundaries
.pageFling(true) // make a fling change only a single page like ViewPager
.onLoad(new OnLoadCompleteListener() {
@Override
public void loadComplete(int nbPages) {
progressBar.setVisibility(View.GONE);
}
})
.load();
}
}
如何调用这个类?
new RetrivePDFfromUrl().execute("Your Pdf Url");