Issue
I’m making an Android app that lets users store a bunch of video files on disk after retrieving them from an RSS feed. The app will run in a web view. I’m fetching the videos with XHR requests.
How can I send a message from javascript to java containing the data for the downloaded files?
I mean … I could store the files within indexdb within the web view browser. However, browser cache APIs like app cache and indexdb have strict quotas. I don’t think I can store many video files within those quotas.
And it also looks like the quota updater API is deprecated, so I can’t alter web storage quotas: http://developer.android.com/reference/android/webkit/WebStorage.QuotaUpdater.html
It seems like my only alternative is to send file data from the web view as a message for java code to handle (and store the file on local disk). Later, I can have java tell the web view that it had previously stored data. How can I send such a message using the android API?
Solution
I’m fetching the videos with XHR requests
Off the cuff, I wouldn't. Usually video files are large, and if you really want them downloaded, you can't just do that from an activity, whether powered by a WebView
or not.
It seems like my only alternative is to send file data from the web view as a message for java code to handle (and store the file on local disk). Later, I can have java tell the web view that it had previously stored data. How can I send such a message using the android API?
Use addJavascriptInterface()
on the WebView
to inject a Java object into the JavaScript environment of the WebView
. On that Java object, have a method that will download the video file, using a Service
(perhaps an IntentService
) or DownloadManager
, given the URL to the file. Not only will this solve your issue of how to get the file stored locally on the device, but it is more likely that the video will actually successfully download if the user navigates away from your app while the download is going on.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.