Android Question Query regarding webview

saeed10051

Active Member
Licensed User
Longtime User
Dear All
i am using webview in an application to download and open an online pdf book. I am using following code, it opens the book in a pdf viewer
B4X:
WebView1.LoadUrl("https://d1.islamhouse.com/data/pdf-viewer/web/viewer.html?file=https://d1.islamhouse.com/data/ur/ih_books/chain/Sahih_Al-Bukhari/ur_bukhari_01.pdf&embedded=true")
My problem is that everytime when the button is clicked to run above code, it downloads this whole book (several mb file). Is there a way that once it is downloaded, the next time when we click the button, it checks that the file is already downloaded earlier and loads it from the application folder instead of downloading everytime
 

drgottjr

Expert
Licensed User
Longtime User
no clean solution for you. you have some workarounds, but the results will not be exactly what you're looking for.

it is possible to make the webview load from cache, but you will have to download the document at least the first time.

the only way to open a .pdf file is with an intent, which means the document will be seen in some sort of .pdf viewer, not a webview. you could download the document in your browser and include it in your app's assets folder. you could then run your intent to view it. in other words, the app never has to download the document.
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
Thanks drgottjr, i am ok with downloading the document first time. As you have told that it is possible for webview to load from cache, can you tell me how to do that. I was thinking about downloading the document and putting it in my assett folder, but my app size will be quite big if i do that, as there are mulitple big documents.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you have to make the choice regarding webview or intent. just to make sure you understand, you cannot load a pdf file from a local file into a webview.
if you are determined to use a webview, you also have to remember to be online to download the pdf the first time. what happens if you cannot get online?
what happens if the server is down? if you download the pdf right now from your desktop browser and copy it to your assets folder, then you can use it whenever you want without being online. you just can't use a webview. you have to use a pdf viewer.

if you are determined to use a webview, then you need add the webviewsettings library to your project. choose the setcachemode method and use "LOAD_CACHE_ELSE_NETWORK". this should let you load the resource the first time and then use the cached resource. webview is a strange beast; there is no guarantee it does what the documentation says it's supposed to do. you will have to try it. it is my understanding that the cache will be cleared when your app is killed by the os (or when you turn your device off). you will need to try things. think hard; you will figure out how to tell if the pdf is downloading each time

if you use okhttputils2 to download the file (a perfectly fine course of action), it will not help you for loading into a webview. you will have to use the saved copy with an intent. mc73 is suggesting you download from the app, i'm suggesting you just download the file now and copy it to your assets folder. the end result is the same: you will have the pdf saved, but you can't use it with a webview. you have to get that idea out of your head.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…