How to get pictures from google picture search page in Android - android

I need to implement component that shows list of pictures related to given search query. Pictures are loading from google picture search.
What can I use instead of google picture search?

Parse json from
https://developers.google.com/image-search/?hl=ru (deprecated and limited)
https://developers.google.com/custom-search/docs/overview (free but with limitations)
Run search query in hidden WebView and get list
of images after page loading.
enter code here
query = "https://www.google.com.ua/search?safe=on&site=imghp&tbm=isch&q=milk+buy";
web = (WebView) v.findViewById(R.id.web);
web.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
}
// you tell the webclient you want to catch when a url is about to
// load
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
// here you get all links to pictures
#Override
public void onLoadResource(WebView view, String url) {
Log.i("web link", String.format(url));
if (url.compareTo(query) != 0) {
images.add(url);
}
}
});
web.loadUrl(query);

Check out this example on Github:
https://github.com/koush/ion/blob/master/ion-sample/src/com/koushikdutta/ion/sample/GoogleImageSearch.java

Related

Unable to open google play store from android webview

I am opening a url in android webview which is redirecting the user to google play store web page in android webview and play store page is showing OPEN IN PLAY STORE APP
and while clicking that button rather then opining play store app webview is redirecting to a web url which is not found.
Now i don't know why it is happening as in google chrome app same scenario is working fine. Please suggest the better solution.
Some code i have done :
mWebView.loadUrl("https://play.google.com/store/apps/details?id=in.org.npci.upiapp&hl=en")
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
return super.shouldOverrideUrlLoading(view, urlNewString);
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
Note: I am using this play store url just as an example.
You need to override shouldOverrideUrlLoading, but you must also inspect the URL and conditionally create an intent if it starts with the intent:// scheme. We (the Android WebView team) have written a sample application which does precisely this.
Just put this data in the webview:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("https://play.google.com/store/apps/developer?id=putyourplaystoreid")) {
Intent i = new Intent("android.intent.action.VIEW");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setData(Uri.parse(url));
startActivity(i);
return true;
} else {
return false;
}
}

Detecting which link opened in chrome custom tab

In webView we can detect user opend which link like following code:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
);
but in custom chrome tab i can't find a way to detect which link opend?
is it possible have control on internal link like webView in CustomTab?
thanks.
It's not possible to track the user navigation that's happening inside a Custom Tab from the host application.

How to disable load images in WebView except certain image?

I want to prevent WebView from loading images except certain image that starts with specific URL.
How to?
Thank you.
This is what you need to do -
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.contains("dummyURL"){ // put a string that is common in urls that you want to load
}else {
view.stopLoading();
}
return false;
}
});

Load URL by .png from a webview

After I've filtered out the extension inside my webview, ".png". How can I then use the entire link to open a new activity?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".png")) {
Toast.makeText(getActivity(), "png clicked", Toast.LENGTH_SHORT).show();
Instead of having my toast, I want to load the URL that was clicked. I'm new to programming in general so I'm not sure the approach.
I would really like to know the use case, but assuming that what you want is just to load that PNG in the webview, you could do this:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".png")) {
view.loadUrl(url);
}
return true;
}
Kind regards

WebView loads url more than a time in android

webview in android loads more than once while loading the url.
Below is the code.
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.contains(".pdf")) {
String[] spliturl = url.split("http://someurl/");
String googleurl = "http://docs.google.com/viewer?embedded=true&url=";
System.out.println("Google Url"+googleurl);
System.out.println("spliturl"+spliturl[1]);
view.loadUrl(googleurl+spliturl[1]);
}
else
view.loadUrl(url);
return true;
}
});
I am splitting the url as it contains more than one url to be passed on google document viewer for rendering the pdf document.
First time the url is correctly split and the url is concatenated to open in google docs but the webview executes again there by giving an ArrayIndexOutOfBoundsException at spliturl[1].
Could anybody let me know why is this executing again.
thanks.
I don't know why it gets called multiple times, but the solution is to handle it in onPageStarted rather than in shouldOverrideUrlLoading
boolean calledOnce=false;
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (url.contains(".pdf") && !calledOnce) {
String[] spliturl = url.split("http://someurl/");
String googleurl = "http://docs.google.com/viewer?embedded=true&url=";
System.out.println("Google Url"+googleurl);
System.out.println("spliturl"+spliturl[1]);
url = googleurl+spliturl[1];
calledOnce = true;
}
super.onPageStarted(view, url, favicon);
}
You should always check if an array has a size more than the index requested:
if (url.contains(".pdf") && url.split("http://someurl/").size()>2){
// your code
}
Don't know why it gets called though - probably multiple redirections.

Categories

Resources