had been done before. NET development, recently need to get a news app, the ability is limited, can only use HTML5 webapp+android Studio to complete this work.
Android Studio mainly uses WebView to load the released WebApp, packaging and manufacturing apps.
Because of the display of some PDF documents, so the research, record the experience, but also hope to help the novice.
Android Display Network PDF, the rationale: First download the pdf file through Downloadmanager to a mobile Phone SDK folder, and then through the Android-pdf-viewer plug-in display.
Android-pdf-viewer Plug-ins can be downloaded directly to GitHub, address: https://github.com/barteksc/AndroidPdfViewer
Or go directly to the published page to download: https://github.com/barteksc/AndroidPdfViewer/releases
I downloaded the: AndroidPdfViewer-2.1.0 version of the ZIP package
Unzip the download AndroidPdfViewer-2.1.0 package and, in your app project, open file-> New-> Import Module and select to the folder you just unzipped.
My catalogue is: D:\AndroidStudioProjects\AndroidPdfViewer-2.1.0\android-pdf-viewer
When you are done, you are prompted to error first:
Error:plugin with id ' com.github.dcendents.android-maven ' is not found.
Workaround:
Click on your project Gradle Scripts directory Bulid.gradle (project: your project name)
In
Buildscript {
repositories {
jcenter ()
}
dependencies {
classpath ' com.android.tools.build: gradle:2.0.0 '
}
}
Add classpath ' com.github.dcendents:android-maven-gradle-plugin:1.3 ' to the dependencies in the
After Mark modules, a new error appears:
Error:plugin with id ' com.jfrog.bintray ' is not found.
At this point also add under dependencies: Classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0"
Mark modules will not be able to complain again.
You can start using the Android-pdf-viewer plugin first,
Don't forget to cite permission issues.
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android: Name= "Android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name= " Android.permission.INTERNET "/>
<uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/>
<uses-permission android:name= android.permission.READ_EXTERNAL_STORAGE"/>
< Uses-permission android:name= "Android.permission.DOWNLOAD_WITHOUT_NOTIFICATION"/>
<uses-permission Android:name= "Android.permission.INSTALL_PACKAGES"/>
First code for the project in the Activity.java, the implementation of receiving another activity to jump over the same time pass the parameters of the PDF address, and then through the Downloadmanager download completed, display PDF.
Package cn.cgrs.myphone;
Import Android.app.DownloadManager;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import android.content.SharedPreferences;
Import Android.database.Cursor;
Import Android.net.Uri;
Import android.os.Environment;
Import Android.preference.PreferenceManager;
Import Android.provider.OpenableColumns;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.webkit.MimeTypeMap;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import Android.widget.TextView;
Import Android.widget.Toast;
Import Com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
Import Com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
Import Com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
Import com.shockwave.pdfium.PdfDocument;
Import Java.io.File; Import Java.io.UnsupportedEnCodingexception;
Import Java.net.URLEncoder;
Import java.util.List; Public class extends Appcompatactivity implements Onpagechangelistener, Onloadcompletelistener {private
Com.github.barteksc.pdfviewer.PDFView Pdfview;
Private TextView TextView;
Private Downloadmanager Downloadmanager;
Private Sharedpreferences prefs;
private static String dl_id = "Downloadid";
Integer pagenumber = 0;
String pdffilename = "Yyy.pdf";
URI Uri;
String Aurl;
Private Boolean Isdown = false; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_report); Intent Intent = Getintent (); Get Intent Aurl = Intent.getstringextra ("url");
Gets the String value log.e ("Receive URL:", aurl);
Aurl = "Http://www.tyyq.cn/xhsapp/download/a03790b7f27243eeada01537a2ce2f77.pdf";
string[] exts = Aurl.split ("/");
Pdffilename = Exts[exts.length-1];
LOG.E ("PDF filename:", pdffilename);
String pdfname = environment.getexternalstoragedirectory () + "/download"; File File = new file (Pdfname, pdffilename);
Pdfview = (Com.github.barteksc.pdfviewer.PDFView) Findviewbyid (R.id.pdfview);
TextView = (TextView) Findviewbyid (R.id.textview); if (file.exists ()) {LOG.E ("Tip:", "Report already exists!")
");
File already exists, the URI is directly displayed = Uri.fromfile (file);
Displayfromuri (URI);
Textview.setvisibility (View.gone);
Pdfview.setvisibility (view.visible);
} else{Isdown = true;}//log.e ("Prefs string:", prefs.tostring ()); @Override protected void Onresume () {Super.onresume (), if (Isdown) {try{log.e ("Tip:", "report does not exist, need to download!").
");
dl_id = Pdffilename;
File does not exist need to download Downloadmanager = (Downloadmanager) getsystemservice (download_service) first;
Prefs = Preferencemanager.getdefaultsharedpreferences (this);
Startreport ();
The catch (Exception ex) {Toast.maketext (this, Ex.getmessage (), Toast.length_long). Show ():}
@Override protected void OnPause () {//TODO auto-generated Method Stub super.onpause ();//unregisterreceiver (receiver); try {unregisterreceiver (receiver);} catch (IllegalArgumentException e) {if (e.getmessAge (). Contains (' Receiver not registered ')) {//Ignore this exception.
This is exactly what is desired} else {//unexpected, re-throw throw e;}} ///Download report start function protected void Startreport () {//TODO auto-generated Method stub if (!prefs.contains (dl_id) | | true) {///
String url = aurl;
LOG.E ("-----", aurl);
string[] exts = Url.split ("/");
Pdffilename = "97b49c0822c14a01b3ebc273679bc6bf.pdf";
EXTS[EXTS.LENGTH-1];
String url = aurl;//"http://www.tyyq.cn/RollImage/11.pdf";
LOG.E ("Start download URL:", aurl);
Start Download Uri resource = Uri.parse (ENCODEGB (URL));
Downloadmanager.request Request = new Downloadmanager.request (Resource); Request.setallowednetworktypes (DownloadManager.Request.NETWORK_MOBILE |
DownloadManager.Request.NETWORK_WIFI);
Request.setallowedoverroaming (FALSE);
Set File type Mimetypemap Mimetypemap = Mimetypemap.getsingleton ();
String mimestring = mimetypemap.getmimetypefromextension (mimetypemap.getfileextensionfromurl (URL));
Request.setmimetype (mimestring); Display//Set notification bar title in the notification bar RequeSt.setnotificationvisibility (DownloadManager.Request.VISIBILITY_VISIBLE);
Request.setdescription ("Public opinion report is downloading");
Request.setshowrunningnotification (TRUE);
Request.setvisibleindownloadsui (TRUE);
SDcard Directory of the download folder Request.setdestinationinexternalpublicdir ("/download/", pdffilename);
Request.settitle ("Public opinion Report");
Long id = downloadmanager.enqueue (request);
Save ID Prefs.edit (). Putlong (dl_id, id). commit (); else {LOG.E ("Hint:", "Download already started, check status");///download already started, check status Querydownloadstatus ();} registerreceiver (receiver, new Intentfilter
(Downloadmanager.action_download_complete));
/** * If the server does not support the Chinese path, the encoding of the URL needs to be converted. * @param String * @return/public string ENCODEGB (string string) {//Convert Chinese encoding string split[] = String.Split ("/"); for (int i = 1; i < split.length; i++) {try {split[i] = Urlencoder.encode (Split[i], "GB2312");} catch (Unsupportedencodingexception e) {E.printstacktrac
E ();
} split[0] = split[0]+ "/" +split[i];
} Split[0] = Split[0].replaceall ("\\+", "%20");//Processing space return split[0]; } privatE broadcastreceiver receiver = new Broadcastreceiver () {@Override public void onreceive (context context, Intent Intent) { Here you can get the ID of the download so you know which file download is complete.
Apply with multiple download tasks for listening log.v ("intent", "" "+intent.getlongextra (downloadmanager.extra_download_id, 0));
Querydownloadstatus ();
}
}; private void Querydownloadstatus () {downloadmanager.query Query = new Downloadmanager.query (); Query.setfilterbyid (
Prefs.getlong (dl_id, 0));
Cursor C = downloadmanager.query (query); if (C.movetofirst ()) {int status = C.getint (C.getcolumnindex (downloadmanager.column_status)); switch (status) {case
DOWNLOADMANAGER.STATUS_PAUSED:LOG.V ("Down", "status_paused");
Case DOWNLOADMANAGER.STATUS_PENDING:LOG.V ("Down", "status_pending");
Case downloadmanager.status_running://is downloading, do not do anything log.v ("Down", "status_running");
Break
Case downloadmanager.status_successful://Complete LOG.V ("Down", "Download Complete");
String pdfname = environment.getexternalstoragedirectory () + "/download";
File File = new file (Pdfname, pdffilename); URI = Uri.fromfilE (file);
LOG.E ("----", uri.tostring ());
Displayfromuri (URI);
Textview.setvisibility (View.gone);
Pdfview.setvisibility (view.visible);
Break
Case downloadmanager.status_failed://Clear downloaded content, download log.v again ("Down", "status_failed");
Downloadmanager.remove (Prefs.getlong (dl_id, 0));
Prefs.edit (). Clear (). commit ();
Break /*pdf Display Function Collection */private void Displayfromuri (Uri urii) {//pdffilename = GetFileName (Urii); Pdfview.fromuri (Urii). Ultpage (pagenumber). Onpagechange (This). Enableannotationrendering (True)-onLoad (This). Scrollhandle (New
Defaultscrollhandle (this)). Load (); public void Onresult (int resultcode, Intent Intent) {if (ResultCode = = RESULT_OK) {uri = Intent.getdata (); displayfrom
Uri (URI); @Override public void onpagechanged (int page, int pagecount) {pagenumber = page; Settitle (String.Format ("%s%s/%s",
Pdffilename, page + 1, pagecount)); Public String GetFileName (Uri uri) {string result = null; if (Uri.getscheme (). Equals ("content")) {Cursor Cursor = getc Ontentresolver(). query (URI, NULL, NULL, NULL, NULL); try {if (cursor!= null && cursor.movetofirst ()) {result = Cursor.getstring Cursor.getcolumnindex (openablecolum Ns.
Display_name));
Finally {if (cursor!= null) {Cursor.close ()}}}}
if (result = = null) {result = Uri.getlastpathsegment (); @Override public void LoadComplete (int nbpages) {Pdfdocument.meta Meta = Pdfview.getdocumentmeta (); printbookmarkstree
(Pdfview.gettableofcontents (), "-"); public void Printbookmarkstree (list<pdfdocument.bookmark> tree, String Sep) {for (Pdfdocument.bookmark b:tree) {//LOG.E (TAG, String.Format ("%s%s, p%d", Sep, B.gettitle (), B.getpageidx ()); if (B.haschildren ()) {Printbookmarkstre
E (B.getchildren (), Sep + "-"); }
}
}
}
The above is a small set to introduce the Android PDF viewer in the Android Studio application problem explanation, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!