Mozilla PDF extremely slow on Samsung Devices - android

I have an application in which I am using Mozilla PDF for viewing PDFs.
Mozilla PDF uses web view to load PDFs.
The PDF files are large, and are all images actually.
The application works fine, but the PDF loads extremely slow on all Samsung devices. The application works fine on other devices, but is so slow on Samsung that it's really useless.
I have the following settings for the web view:
settings.setJavaScriptEnabled(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I also have set a WebChromeClient.
Does anyone knows why am I facing this issue? And only on Samsung devices?
Any workarounds? Any help would be greatly appreciated.

Here’s a WebView setup to display PDFs using PDF.js. It works fine on all devices (that are running at least API 19), including old tiny about-to-die Samsungs.
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
webView.getSettings().setAllowFileAccessFromFileURLs(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
// should enable pinch to zoom....
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
String webViewUrl;
try
{
/*
Try encoding the PDF file URL. I think i’ve read that this helps with
load/rendering speed?
*/
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + URLEncoder
.encode(pdfUri.toString(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
// It works without encoding it (at least in my case) so just display it with the string as is.
Log.e("Error encoding PDF file URL, will display without encoding.", e);
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + pdfUri.toString();
}
webView.loadUrl(webViewUrl);
Not sure if this will help, but thought I’d post a full WebView setup to support PDF.js, which I’ve never run into speed issues with. Good luck.
Note: Might also be a good idea to turn hardware acceleration on in your application’s manifest. This is only available on API 14 and above. By now this shouldn’t be an issue (if it is, my heart goes out to you).
<application
android:name="com.pdfjs.webview.app.McAwesome"
//...
android:hardwareAccelerated="true">
</application>

Related

How to stream swf file in android WebView

My app is supporting min api level 15 and I want to show a list which streams video from remote servers and video can be of any file format, thus I am considering the WebView as best choice but I am unable to stream swf file in WebView. And also for other files the WebView is showing jumping effect in ListView after loading the Videos.
Can you show us what have you already tried? Some code would be useful in order to understand what could be the problem.
Until then, you could try the following:
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
mWebView.getSettings().setPluginsEnabled(true);
}
else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2){
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
}
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setBackgroundColor(Color.parseColor("#000000"));
mWebView.loadUrl("http://your_file_location.swf");

Android WebView is taking more time to load website then the browser in laptop, both laptop and android mobile are using same internet connection

Is there something I am missing to set some property of webview?
I have set following property but no improvement in performance:
webview.setWebViewClient(new myWebClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
// Enable the caching for web view
webview.getSettings().setAppCacheEnabled(true);
// Specify the app cache path
webview.getSettings().setAppCachePath(getCacheDir().getPath());
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
if (Build.VERSION.SDK_INT >= 19) {
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
Any help will be highly appreciated, Thanks in advance.
Actually you are using webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); which disallows any kind of caching in your Android WebView.
While browsing in browser, most of your content is loaded from cache, which results in difference of loading time. Hence, affects overall performance.
You should use it as follows to improve caching:
webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

Android WebView cache doesn't expire

in my Android App I use a WebView and got a problem with caching.
After a file is cached the WebView just keeps it forever (or at least until I delete the cache manually).
In the Apache Access-Log I see that the WebView doesn't send requests once the file is cached. How can I setup the WebView to send requests for checking if the file is expired by using common ETags and HTTP-Code 304? On a desktop browser(Firefox, Chrome) the caching works as expected.
Here is my code:
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new EWebViewClient(this));
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.setWebChromeClient(new EWebChromeClient(this));
webView.setBackgroundColor(Color.argb(1,0,0,0));
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.loadUrl(url);
EWebViewClient only overwrites: "shouldOverrideUrlLoading, onPageFinished, onLoadResource, onReceivedError"
I really appreciate any help you can provide.

localStorage html5 feature not working in WebView on Samsung Android device

I have a html5 application that I wrap with a WebView. To store and retrieve user input values between pages, I use the localStorage html5 feature.
It works fine on my Nexus 4 (Android 4.4.4), but it does not work on Samsung Galaxy Tab 2 (Android 4.3.x) (= nothing happens, but also no error in logcat).
Or, to be more clear: on Samsung, it does not work if the html pages are loaded from within the app's asset folder. It does work though if I put the pages on a server, as below in the outcommented line.
However, on Nexus 4, loading from file:///android_asset/ and also if I load the pages on a desktop browser (Chrome, Firefox) from file:// path, it is also working.
Update 1: I just had another user who reported the issue with a LG device, so it does not seem to be Samsung specific.
Update 2: Storing and loading the value from localStorage works fine on all devices on the same page, however, not between different pages. In my example, I can store and retrieve the value on 01_home.html, but when I go to another page in the android_asset folder, I cannot read it anymore (on LG, Samsung devices). Works fine on Nexus 4 though.
Below are the settings of the web view.
webView = (WebView)this.findViewById(R.id.webView);
webViewClient = new MyWebViewClient(this);
webViewClient.setSm(sm);
webView.setWebViewClient(webViewClient);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
webView.loadUrl("file:///android_asset/01_home.html"); // does NOT work!
// webView.loadUrl("http://192.168.178.33/01_home.html"); // does work!
Local storage code in the pages:
// storing
var data = document.getElementById('data').value;
window.localStorage.setItem((1), data);
// reading
document.getElementById('data').value = window.localStorage.getItem(1);
There are answers to this posted elsewhere Android webview & localStorage
Solution proposed:
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
}
I found this same bug. Worked on all Nexus devices but all Samsung devices lost my user changes. I know the below method is deprecated but it solved my issue.
webview.getSettings().setDatabasePath("/data/data/" + webview.getContext().getPackageName() + "/databases/");

How to run the HTML5 video in webview in android

From last one week i try to find how to play a html5 video in android webview i read so many blog and questions in the stack overflow but my problem is solve.i am using .mp4 , .wevm ,ogv and also .3gp format but noon of format is running.
i run these videos in the two different device one is micro-max funbook with android 4.0 and the another device is a Samsung device with 2.3.3 android os.
this is my code.
url="file://"+Environment.getExternalStorageDirectory()+"/unzipped/HTML5/res/test1.html";
webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl(url);
For API level 11+ it should suffice that you add the attribute android:hardwareAccelerated="true" to the <application> tag in your Android manifest file.
For older APIs you need to implement the methods onShowCustomView and onHideCustomView in your your own WebChromeClient.
Or you can check out the html5webview

Categories

Resources