Sunday, 10 August 2014

Using Getty Images Api To get Image url in Android


Getty Images, Inc. is an American stock photo agency, based in Seattle, Washington, United States. It is a supplier of stock images for business and consumers with an archive of 80 million still images and illustrations and more than 50,000 hours of stock film footage. It targets three markets—creative professionals (advertising and graphic design), the media (print and online publishing), and corporate (in-house design, marketing and communication departments).
Getty has distribution offices around the world and capitalizes on the Internet and CD-ROM collections for distribution. As Getty has acquired other older photo agencies and archives, it has digitised their collections, enabling online distribution. Getty Images now operates a large commercial website which allows clients to search and browse for images, purchase usage rights and download images. Costs of images vary according to the chosen resolution and type of rights associated with each image. The company also offers custom photo services for corporate clients.

Getting Started

If you already have a Mashery Member account

  1. Sign in with your Mashery Member credentials.
  2. Click the My Account link near the top right of the page.
  3. Click the Get API keys button.
  4. Register your application and select your desired type of Api-Key. Getty Images provide two key options.
    • Issue a new key for Getty Test
      • Use to test Getty Images Connect functionality including: image search and metadata, download, and account management.
    • Issue a new key for Connect Embed
      • Use to search for and embed from over 40 million embeddable images.

If you do not have a Mashery Member account

  1. Register a new Mashery Member account and your application.
  2. Select your desired type of Api-Key. Getty Images provide two key options.
    • Issue a new key for Getty Test
      • Use to test Getty Images Connect functionality including: image search and metadata, download, and account management.
    • Issue a new key for Connect Embed
      • Use to search for and embed from over 40 million embeddable images.
  3. Click Register. You will receive an email presently with a confirmation link. Click the link.
  4. Sign in with your Mashery Member credentials.


After registering a application you will receive an api-key . You need this key to authenticate the requests that you make to connect.gettyimages.com .


Now open your android project and add the following to your strings.xml file .

<string name="getty_id">Your api-key</string>

Getting the Image URL

Now we will search for images and get their url . Add the following function where you want to search for image . This function makes request to "https://connect.gettyimages.com/v3/search/images" with the api key in header . In this example I'll be using volley library for making requests

Pagination
Many Connect endpoints provide support for pagination of results. Pagination can be controlled by using the page and page_size query string parameters. Default values will be used if none are provided.

The page parameter defaults to 1.
Search results default to a page_size of 30 items per page. The maximum page_size is 100.

Here I have selected the first page and result per page is also 1 , you can change these parameters depending on your needs .

private void getBackgroundImage() {  
           // TODO Auto-generated method stub  
           String searchString = "What you want to search";  
           final HashMap<String, String> header = new HashMap<String, String>();  
           header.put("Api-Key", getString(R.string.getty_id));  
           String getImageUrl = null;  
           try {  
                getImageUrl = "https://connect.gettyimages.com/v3/search/images?phrase="+URLEncoder.encode(searchString.replaceFirst("\\s+$", ""),"UTF-8")+"&page=1&page_size=1";  
           } catch (UnsupportedEncodingException e1) {  
                // TODO Auto-generated catch block  
                e1.printStackTrace();  
           }  
           JsonObjectRequest getImageData = new JsonObjectRequest(getImageUrl, null, new Response.Listener<JSONObject>(  
                     ) {  
                @SuppressLint("NewApi") @Override  
                public void onResponse(JSONObject response) {  
                     try {  
                          onResponseGetImages(response);  
                     } catch (JSONException e) {  
                          // TODO Auto-generated catch block  
                          e.printStackTrace();  
                     }  
                }  
           }, new Response.ErrorListener() {  
                @Override  
                public void onErrorResponse(VolleyError error) {  
                     // TODO Auto-generated method stub  
                }  
           }){  
                @Override  
                public Map<String, String> getHeaders() throws AuthFailureError {  
                     return header;  
                }  
           };  
           AppController.getInstance().addToRequestQueue(getImageData, "Get Image Data");  
      }  


Getting Url From Response

The response is in JSON format and we need to parse this to get image Url . 
This is how the response from  https://connect.gettyimages.com/v3/search/images looks like .



Now to get Url of the image from the response add the following function below the getBackgroundImage function that you added above . In the following function I'm checking that the result count (gives the total number of images found relating to the search ) is greater than 0 . If result count is 0 it means that no images were found relating to that result . In the images array I'm taking the first JSON Object (index 0) , and then get the display sizes array in it which has the image url in it .

private void onResponseGetImages(JSONObject response)  
                throws JSONException {  
           int resultCount = Integer.parseInt(response.getString("result_count"));  
           if(resultCount>0){  
           JSONArray imagesArray = response.getJSONArray("images");  
           JSONObject temp = imagesArray.getJSONObject(0);  
           JSONArray displaySizesArray = temp.getJSONArray("display_sizes");  
           JSONObject temp4 = displaySizesArray.getJSONObject(0);  
           String imageUr = temp4.getString("uri");  
           Toast.makeText(getActivity(),  
                     ""+imageUr, Toast.LENGTH_SHORT)  
                     .show();  
           }else{  
                Toast.makeText(getActivity(),  
                          "No Images Found !", Toast.LENGTH_SHORT)  
                          .show();  
           }  
                }  


Now you can use this url to download or get the bitmap of the image .








1 comment: