I have tried to open a locally saved PDF file in a WebView. I can open it by using IntentService, but I want to open it in a WebView. Except for PDFs, all other files are working for me.
I have used the codes below:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setAllowFileAccess(true);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(mFilePath));
webView.loadData(mFilePath, mimeType, "UTF-8");
Can anyone please help? Thanks in advance.
You can use Google Docs Viewer to read your pdf online:
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
You can also use iframes.
pdfView.setWebChromeClient(new WebChromeClient());
WebSettings settings = pdfView.getSettings();
settings.setPluginState(WebSettings.PluginState.ON);
pdfView.setWebViewClient(new WebViewClient());
pdfView.setBackgroundColor(0x00000000);
settings.setAllowFileAccess(false);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(false);
settings.setSaveFormData(false);
settings.setJavaScriptEnabled(true);
String html = "<iframe src=\"http://docs.google.com/gview?url=" +
path + "&embedded=true&wmode=opaque\"" +
"style=\"width:600px; height:900px;\" scrolling=\"no\"" +
"frameborder=\"0\" allowfullscreen></iframe>";
pdfView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
Related
I am trying below approach but it's taking around 10sec to load page even I cached the page.
**webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setUseWideViewPort(true);
webView.addJavascriptInterface(new
WebAppInterface(OfflineReportActivity.this), "android");
webView.setWebViewClient(new Client());
webView.setWebChromeClient(new ChromeClient());
webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ONLY);**
you can load html file into your webview as url
WebView lWebView = (WebView)findViewById(R.id.webView);
File lFile = new File(Environment.getExternalStorageDirectory() + "<FOLDER_PATH_TO_FILE>/<FILE_NAME>");
lWebView.loadUrl("file:///" + lFile.getAbsolutePath());
webview showing white screen when loading local pdf
WebView webView = (WebView)findViewById(R.id.documentView);
webView.invalidate();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadData(path.toString(),"application/pdf", "UTF-8"
);
please help me.
try this
String localPath = path.toString();
localPath = localPath.replace(" ", "%20").trim();
and use it like this
WebView webView = (WebView)findViewById(R.id.documentView);
webView.invalidate();
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadData("file:///"+localPath,"application/pdf", "UTF-8");
i want to show the images from http not stored in local. am not able to show images.
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<html><body>"
+ "<h1>Hello, WebView</h1>"
+"<img src="+"'http://dreamatico.com/data_images/flowers/flowers-5.jpg'"+" height="+142+" width="+142+">"
+ "</body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
Create the html file and put it in the assets folder and load that html file using
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/new.html");
I am testing with a web content just using tags to show videos.
the content is rendered properly in chrome.
But for the same audio is not being played in the web view.
My code for the web view
Intent intent = getIntent();
Uri webUri = intent.getData();
WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.setSoundEffectsEnabled(true);
webView.setWebViewClient(new CustomWebViewClient());
//webView.setWebChromeClient(new WebChromeClient());
Log.i(TAG, "User Agent used in the web view " + webViewSettings.getUserAgentString());
Log.i(TAG, "URL to be loaded " + webUri.toString());
webView.loadUrl(webUri.toString());
We just use video tags so there should not be any incompatibility problem.
Thanks and Regards,
Saurav
I have downloaded map750.png file in the root of sdcard, but when I try to show it within a webview with some text, only the text is shown. Could you help me to find what is wrong in the code? thanks.
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String html = "<html><head></head><body><p>hello</p><img src=\"file://mnt/sdcard/map750.png\" alt=\"alternativo\" /></body></html>";
mWebView.loadData(html, "text/html","utf-8");
I edit the post to add the solution suggested by oneilse14, thanks!!
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String html = "<html><head></head><body><p>hello</p><img src=\"file://mnt/sdcard/map750.png\" alt=\"alternativo\" /></body></html>";
mWebView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
Try this,
final String fileName = "file:///mnt/sdcard/1.jpg";
final String mimeType = "text/html";
final String encoding = "utf-8";
final String html = "<img src=\""+fileName+"\">";
webView.loadDataWithBaseURL("", html, mimeType, encoding, "");
Check out loadDataWithBaseURL() in the Developer docs
http://developer.android.com/reference/android/webkit/WebView.html
loadData() has certain restrictions on what it can display. This method is able to display local device files like you are trying to do.
Note that the access using the file:// schema can fail due security restrictions.
Another approach is to use the URI of a ContentProvider that handles the local file access. Overwrite openFile(Uri, String) in a subclass of ContentProvider for this approach.