how to display pdf from localhost - android

In my application i need to display pdf files.
I tried to display in webview using google docs. My problem is pdf files are stored in localhost. when give this localhost url its not working. But other all urls are works fine. I checked my localhost url in browser its work fine. I can't able to see in my application.
It shows like this
" Sorry, we were unable to find the document at the original source.Verify that document still exits. you can also try to download the original document by clicking here "
My code
runOnUiThread(new Runnable() {
public void run() {
WebView mWebView=new WebView(PdfFiles.this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
String PdfUrl = "http://10.0.2.2/moodle/practice/document/"+Pdfname;
mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+PdfUrl);
//"http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf"
//mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+"http://www.ztsinc.com/MBT1_OI.pdf");
//mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+"http://www.ourwebsite/path"+lvForDialog.getItemAtPosition(position));
setContentView(mWebView);
}
});

Related

Google Document Viewer shows “No Preview Available”

I have a problem with google doc viewer for display xls file get from my server.
Currently when i try to display my xls file get from my server in my android app with a webview, i have a grey page with no preview available
(I can download the xls from my server with the url).
Sorry, I could not share link but It's like xxxx.com/123/DownloadExcel_GET?paraOne=%s&paraTwo=%s&paraThree=%s.
But when i try with xls exemple file in internet like http://lecompagnon.info/demos/demoxl3.xls , i don't have this problem. The xls is display succesfully.
Same it's working if i try with a googleDrive url like "drive.google.com/file/t/myfile/view?usp=sharing".
The only difference with the exemple file in internet and my file from my server, is the time to start the download.
With the exemple file is instant, but with my xls i have to wait more than 10sec to start the download.
I think google doc viewer return no preview available, because for him 10sec to get the file is too long and he return a time out. But it's a supposition...
I found nothing about this problem, only this topic Google Document Viewer shows "No Preview Available" .
Here is my code :
String urlgoogle = "https://docs.google.com/gview?embedded=true&url=";
String completeURL = urlgoogle + query;
Log.i("completeURL", completeURL);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(completeURL);
//wv.loadUrl("https://docs.google.com/gview?embedded=true&url=" + "http://lecompagnon.info/demos/demoxl3.xls"); working
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
edit
I add more information.
The grey page with no preview available not always showing, sometimes my xls file is display successfully the first time.
If i have the grey page, i can refresh, and after one or two refresh, the file is display. The refresh just call again the url in the webview
The file is dynamically create and generated from the serveur, this is why for download or get the file i must wait 10 sec.
Do Google doc viewer have a timeout if the time to get the file to display is too long ?

Android Webview href link not working

Basically, I have a Webview that points to an internal "note.html" file.
In this note.html file, which is written in HTML, I have a website HREF, but when I click on it on a device, I get an error message:
"the webpage at file:///android/asset/www.blahblah.com might be temporarily down ...etc."
I am somewhat aware that I have to parse the URL or something of that nature, and would really appreciate a response on how to get the HREF link to open the proper web link to a browser.
the variable:
private static final String tip_URL = "file:///android_asset/tip.html";
the onCreate:
WebView mywebview = (WebView) findViewById(R.id.webview); mywebview.loadUrl(tip_URL);
this works fine on a mobile device, however, in the tip.html there is a link to a website, and when clicked i want a browser to open to go to that site..it is not another local page..

WebView not loading URL

I want to load a web page in my webView.
Tried placing the webView.loadurl("") in AsyncTask's doinbackground / onpostexecute
and in the onresume.
The url is correct but nothing happens it just shows a white page. In the android manifest file internet access is enabled.
What else needs to be done to load a webview?
The application does not crash or show any error.
In my emulator I set the proxy with my user name and password.
Here is the code I use to load the URL:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClientSubClass());
webView.loadUrl(promoURL);
I would recommend you to check what callbacks your webViewClient is getting. I'm guessing that the site requests an authentication, so override onReceivedHttpAuthRequest and do something like this
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
Log.d(TAG, "onReceivedHttpAuthRequest"));
handler.proceed(username, password);
}
Or if the authentication isn't the problem you can always overide onReceivedSslError to see if there is some certificate problem.
As the initial step though, I would recommend you to use the browser to see if you can load the page. I'm having some trouble with an https site that requires authentication, I enter my credentials and the site can't load(this is on android 2.3)
myVideoView = (WebView) findViewById(R.id.webView1);
myVideoView.setWebViewClient(new WebViewClientSubClass());
myVideoView.getSettings().setJavaScriptEnabled(true);
myVideoView.setPersistentDrawingCache(0);
myVideoView.getSettings().setPluginsEnabled(true);
myVideoView.requestFocus(View.FOCUS_DOWN);
myVideoView.loadUrl(promoUrl);
try this should work and also check whether it opens with https in normal browser.

How do I open another app in my WebView app?

I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.

ExternalInterface.addCallback not working on Android for local files

I have the following situation:
AS3 Code:
...
Security.allowDomain("*");
ExternalInterface.addCallback("onZoom", onZoom);
...
public function onZoom(...
JavaScript code:
...
alert(getFlashMovie("test"));
alert(getFlashMovie("test").onZoom);
...
And in OBJECT and EMBED tags I have allowScriptAccess="always"
If I test the html page from my computer ("file:///path-to-file.html") both
alert(getFlashMovie("test"));
alert(getFlashMovie("test").onZoom);
give me proper result.
If I put my html as well as my swf in my android phone sdcard and I do load it into a WebView like this...
webView = (WebView) findViewById(R.id.webViewMain);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///sdcard/test/test.html");
... then alert(getFlashMovie("test").onZoom); gives me "undefined".
If I move my html and swf to a web server, and load with webView.loadUrl("http:/mydomain.com/test/test.html"); it works.
I did another try with this example:
http://0me.me/demo/adobeflash/ExternalInterface.call/demo.html
It works if I load it from that url, it doesn't if I copy that html file (and the relevant swf) on my sdcard.
If I copy the same file (and swf) on my computer and test it locally, there it works.
So it seems that flash player for computer allows ExternalInterface.addCallback even for local files (file:///...) whereas flash player for android 10.2 doesn't.
I didn't try installing a local web server on my android phone and loading from there so far, but I would avoid such a solution if possible.
Can someone help me please?
Thanks.

Categories

Resources