Sunday, 15 August 2010

reading pdf from url using pdfViewer library in android app -



reading pdf from url using pdfViewer library in android app -

i made android app viewing pdf file fetched url integrating pdfviewer library in code.firstly app downloading file web external sd card there app getting opened pdfviewer library.it working fine if file size little if pdf file contains images , size more , downloaded file size shown in sdcard 0kb. can help me out why so?

following java code :

public class mainactivity extends activity { static context applicationcontext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); applicationcontext = getapplicationcontext(); string extstoragedirectory = environment.getexternalstoragedirectory().tostring(); file folder = new file(extstoragedirectory, "pdfdownloads"); folder.mkdir(); file file = new file(folder, "android.pdf"); seek { if(!file.exists()) { file.createnewfile(); } } grab (ioexception e1) { e1.printstacktrace(); } boolean downloadfile = downloadfile("http://www.irs.gov/pub/irs-pdf/fw4.pdf", file); if (file!=null && file.exists() && file.length() > 0){ intent intent = new intent(this, com.example.soniapdf.second.class); intent.putextra(pdfvieweractivity.extra_pdffilename, file.getabsolutepath()); startactivity(intent); } } public static boolean downloadfile(string fileurl, file directory) { seek { fileoutputstream f = new fileoutputstream(directory); url u = new url(fileurl); httpurlconnection c = (httpurlconnection) u.openconnection(); c.setrequestmethod("get"); c.setdooutput(true); c.connect(); inputstream in = c.getinputstream(); byte[] buffer = new byte[1024]; int len = 0; // int filelength = c.getcontentlength(); long total = 0; // toast.maketext(applicationcontext, "downloading pdf...", 2000).show(); while ((len = in.read(buffer)) > 0) { total += len; //toast.maketext(applicationcontext, "downloading pdf: remaining " + (filelength / total )+ "%", 1).show(); f.write(buffer, 0, len); } f.close(); homecoming true; } grab (exception e) { e.printstacktrace(); homecoming false; } } }

this way showing pdf in android app embedding pdf document android webview using back upwards http://docs.google.com/viewer

pseudo

string doc="<iframe src='http://docs.google.com/viewer?url=+location pdf file+' width='100%' height='100%' style='border: none;'></iframe>";

a sample is shown below

string doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' width='100%' height='100%' style='border: none;'></iframe>";

code

webview wv = (webview)findviewbyid(r.id.webview); wv.getsettings().setjavascriptenabled(true); wv.getsettings().setpluginsenabled(true); wv.getsettings().setallowfileaccess(true); wv.loadurl(doc); //wv.loaddata( doc, "text/html", "utf-8");

and in manifest provide

<uses-permission android:name="android.permission.internet"/>

see answer

edit

if pdf document accessible online, utilize google docs viewer open pdf in webview

refer

wv.loadurl("https://docs.google.com/gview?embedded=true&url=http://www.irs.gov/pub/irs-pdf/fw4.pdf");

don't know how stable these are

here list of other open sources pdf readers running on top of android

android pdf viewer apdfviewer droidreader android-pdf

please note these , other project derived mupdf bound terms of gpl , may not suitable commerical use.

the next list of sdks suitable commerical use:

pdftron adobe qoppa radaee

android

No comments:

Post a Comment