I'm trying to figure out how to find images from the Internet, and show them on Android (as thumbnails and full screen).
Not only Google has deprecated the previous way to do it, but now they have documentation for a new kind called "custom search":
Later, Google has chosen that developers should use "Google Play Services", but again, I can't find the correct commands for this specific task. This is what I've found: https://developer.android.com/google/auth/api-client.html
I've found a lot of links, but don't know which of them are the best that Google suggests, and I also don't know how come there are barely any samples (let alone in Java and search feature) ...
Since everything got merged, and since there is few documentation (and examples), I wish to ask if there is an example of how to achieve searching of images using this new SDK, and maybe if there is a library that makes it easier.
I also wish to ask what will happen for devices that don't have the Google Play services - will it still work?
Only some of the Google APIs are available via Google Play Services library (eg: maps, drive, ads etc). Using Play services library gives you some advantages like easy to use client libraries, common authorization, automatic updates on devices etc.
Custom Search API
currently is not one of them. So we need to use the other option - Google APIs Client Library for Android. Using this, you can access most of Google APIs. Additionally, this does not require client devices having play services.
For a sample test, use android-async-http
and do a below GET request (you will need to get API Key and CX key). eg: Image search for unicorn:
GET https://www.googleapis.com/customsearch/v1?q=unicorn&cx={CX}&searchType=image&key={YOUR_API_KEY}
Sample JSON output for image search looks like :
"items": [
{
"kind": "customsearch#result",
"title": "Unicorn - Monster Wiki - a reason to leave the closet closed and ...",
"htmlTitle": "<b>Unicorn</b> - Monster Wiki - a reason to leave the closet closed and <b>...</b>",
"link": "http://img1.wikia.nocookie.net/__cb20090425203919/monster/images/c/c4/Unicorn.jpg",
"displayLink": "monster.wikia.com",
"snippet": "Unicorn",
"htmlSnippet": "<b>Unicorn</b>",
"mime": "image/jpeg",
"image": {
"contextLink": "http://monster.wikia.com/wiki/Unicorn",
"height": 375,
"width": 500,
"byteSize": 235870,
"thumbnailLink": "https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRcX2jgPxf-0yzrfV5piHuiKpDzSCGpJNbB0vywH-nqvmeL1-gL22uaZjb5",
"thumbnailHeight": 98,
"thumbnailWidth": 130
}
},
...
...
]
Pull out image urls, and use a image loader android library like Picasso
- (not related to google picasa
) to download the thumbnails and full images, for use in your app
Sample code:
create a hello world app in android studio with blank activity. Add below 2 lines of dependencies to build.gradle for "app" module
compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.squareup.picasso:picasso:2.3.4'
add an ImageView
in activity_main.xml for showing result from custom google image search
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:src="@drawable/ic_launcher" />
in onCreate
, do a HTTPS GET
Custom Google Search Image Query using AsyncHttpClient and use Picasso
library to show the first image result in your imageView
String imgQuery = "https://www.googleapis.com/customsearch/v1?q=unicorn&cx={CX_KEY}&searchType=image&key={API_KEY}";
new AsyncHttpClient().get(imgQuery, new AsyncHttpResponseHandler() {
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
String imgJson = new String(response);
String firstImage = imgJson.split("link\": \"")[1].split("\"")[0];
Picasso.with(getApplicationContext()).load(firstImage).into((ImageView) findViewById(R.id.imageView));
}
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {}
});