PDF No Preview Available in android webview - android

I am developing an android app in which I have to load a PDF in a webview by appending http://drive.google.com/viewerng/viewer?embedded=true&url= before the actual URL. But after loading it is not previewing the document and saying that "No Preview Available". But the same document is loaded in iOS webview and the Desktop chrome. At the same time, other PDF files from internet are loading properly. I dont know whether it is the issue with the file or my code. Here is my code
`
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
private String postUrl;
private String doc_type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
this.webView = (WebView) findViewById(R.id.webView);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setHorizontalScrollBarEnabled(false);
this.webView.getSettings().setAllowFileAccess(true);
doc_type = getIntent().getStringExtra("doc_type");
String url = getIntent().getStringExtra("click_action_url");
if (doc_type != null && doc_type.equalsIgnoreCase("others")) {
postUrl = url;
} else{
postUrl = "http://drive.google.com/viewerng/viewer?embedded=true&url=" + url;
}
this.webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
}
});
this.webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
this.webView.loadUrl(postUrl);
}
}`
Someone please help.

I had the same issue, try encoding the url before loading it. I had answered here https://stackoverflow.com/a/67245546/1843984

Related

WebView show message "sorry has err"

I use WebView for play a video from Aparat, that is an online video service like youtube . Although it works well but in higher android version like 10 shows message sorry has err. I want does not show this message.
This is my code:
void webview() {
WebSettings ws = webView.getSettings();
ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
ws.setPluginState(WebSettings.PluginState.ON);
ws.setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.reload();
webView.loadData(Url, "text/html", "UTF-8");
WebClientClass webViewClient = new WebClientClass(progressBar);
webView.setWebViewClient(webViewClient);
WebChromeClient webChromeClient = new WebChromeClient();
webView.setWebChromeClient(webChromeClient);
}
public class WebClientClass extends WebViewClient {
ProgressBar ProgressBar = null;
WebClientClass(android.widget.ProgressBar progressBar) {
ProgressBar = progressBar;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
ProgressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
ProgressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
the example of Url :
<div id="19697445500"><script type="text/JavaScript" src="https://www.aparat.com/embed/PBZfF?data[rnddiv]=19697445500& data[responsive]=yes"></script></div>

Progress Dialog not showing in webview

I have a webview in my android application which renders html pages from a local folder, Now I want to show progress dialog when navigating from one html page to another but the progress dialog which I am using isn't showing up. Here is my code snippet:
#JavascriptInterface
public void save(String respString, boolean ifEndNode) throws JSONException {
ProgressDialog progDialog = null;
try {
if(ifEndNode){
//start loader
progDialog = new ProgressDialog(webView.getContext());
progDialog.setMessage("Saving survey, Please DON'T close the Application!! ");
progDialog.setCanceledOnTouchOutside(false);
progDialog.setCancelable(false);
progDialog.show();
}
//some code here
//...
}
catch (Exception e)
{
Logger.e(context,"exception", "jsonObjectexception");
}
finally {
if(progDialog != null && progDialog.isShowing())
progDialog.dismiss();
}
Can anyone suggest what the problem could be?
You can use WebView with ProgressDialog this way. This is nice and simple approch.
private WebView webView;
ProgressDialog prDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_news);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
webView = (WebView) findViewById(R.id.wv_news);
webView.setWebViewClient(new MyWebViewClient());
String url = "http://google.com/";
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.loadUrl(url);
}
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = new ProgressDialog(NewsActivity.this);
prDialog.setMessage("Please wait ...");
prDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(prDialog!=null){
prDialog.dismiss();
}
}
}

PDF Viewer only show first page properly

I want to make a PDF viewer using a WebView and Google Docs url.
However, for some reason it isn't showed properly when it reaches 2nd page and successive, except on Android 5.
Here is an image from Android 4.1.1:
http://oi57.tinypic.com/6rrbpy.jpg
And here other from Android 5.0.0:
http://oi62.tinypic.com/2vdlefm.jpg
Any idea why this happens?
Here is my code
public class PDFViewer extends Activity {
public void onCreate(Bundle savedInstanceState){
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
this.setContentView(R.layout.web_viewer);
WebView myWebView = (WebView) this.findViewById(R.id.webViewSimple);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (progressDialog == null) {
progressDialog = new ProgressDialog(PDFViewer.this);
progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}
});
String link=getIntent().getExtras().getString("link");
myWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+link);
}

How I get page source from WebView?

How I get the web page's source from WebView?
I want to only enter www.google.com in my webview and When I entered this site, I want to get the source for example
String a=........;(source)
I am not sure how far this is going to be helpful. But I have used the below snippet to fetch a small html page's data. I hope it helps you.
Create a class like the one below,
class MyJavaScriptInterface
{
#SuppressWarnings("unused")
public void processHTML(final String html)
{
Log.i("processed html",html);
Thread OauthFetcher=new Thread(new Runnable() {
#Override
public void run() {
String oAuthDetails=null;
oAuthDetails=Html.fromHtml(html).toString();
Log.i("oAuthDetails",oAuthDetails);
}
});OauthFetcher.start();
}
}
Now in your onCreate(),
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webview.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, final String url) {
String oAuthUrl=getString("www.google.com");
if(url.contains(oAuthUrl))
{
Log.i("Contains","Auth URL");
twitter_webview.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.show();
}
});
And now what happens is that, when your page finishes loading, the JavaScript class will be called, which would retrieve the page source and store it in a String as your requirement.
And For API 17
import android.webkit.JavascriptInterface;
public class MainActivity extends Activity {
final Context myApp = this;
#JavascriptInterface
public void processHTML(String html) {
if (html == null)
return;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(this, "HTMLOUT");
browser.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
browser.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
});
browser.loadUrl("http://www.google.co.il");
}
}
If your minSdkVersion is 19 or greater you can use the following.
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
view?.evaluateJavascript("""(function() {
return "<html>" + document.getElementsByTagName('html')[0].innerHTML + "</html>";
})()""".trimMargin()) {
console.log(it)
}
}

android webview not displaying video using WebViewClient

i want to play a video from youtube on webview.. it displays the video,But i want to play it on the same page i mean i've to use WebViewClient.. but using that it doesn't play the video.. (on pressing play button it doesn't play the video) what should i do? my code is
setContentView(R.layout.main);
wvSpecials = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wvSpecials.getSettings();
webSettings.setJavaScriptEnabled(true);
wvSpecials.loadUrl("http://here.com/is link/");
wvSpecials.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog = new ProgressDialog(
specialsActivity.this);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressDialog.setMessage("Please wait...");
progressDialog.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
super.onPageFinished(view, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
}
You may use the below listed code:
public class YouTube extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView myWebView;
myWebView = (WebView) findViewById( R.id.web);
myWebView.setWebViewClient(new MyWebViewClient());
String pre="<iframe class=youtube-player type=text/html width=";
String height=" height=";
String suffix=" src=http://www.youtube.com/embed/**xxxxxxxxxxx**?autoplay=1 frameborder=0>"; // replace xxxxxxxxxxx with the specific embed id of your video
String playVideo=pre+260+height+150+suffix;
myWebView.getSettings().setPluginsEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadData(playVideo,"text/html","UTF-8");
}
// override default behaviour of the browser
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
ProgressDialog dialog = ProgressDialog.show(getApplicationContext(), "",
"Loading. Please wait...", true);
#Override
public void onPageFinished(WebView view, String url) {
dialog.dismiss();
}
} }

Categories

Resources