Monday, June 6, 2011

Flickr api, random image example

Introduction

SDK Version:  M3
Lately we have been doing all kinds of demo projects, and tests, where we did not want to use constant data to present our products. Having a demo of your application is only nice, if it has relevant data in it. It could be top class technology, but if it is using the same image 20 times in a list, well that does not look very good.
SDK Version:  M3

Lately we have been doing all kinds of demo projects, and tests, where we did not want to use constant data to present our products. Having a demo of your application is only nice, if it has relevant data in it. It could be top class technology, but if it is using the same image 20 times in a list, well that does not look very good.

flickr_logo


Using the Flickr API

The Flickr api has a lot of useful methods, but I only needed one, which is search. I choose the json response, because that's the easiest to work with. It has support for several other protocols/formats.
One problem that I encountered is, that the flickr api does not send a valid json response:

1
2
3
4
jsonFlickrApi({"photos":{"page":1, "pages":73442, "perpage":1, 
"total":"73442", "photo":[{"id":"5138524515", "owner":"12766047@N06", 
"secret":"e000d9791e", "server":"4045", "farm":5, "title":"Gifts under $15 CAD",
"ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"})

As you can see, it has some junk at the beginning "jsonFlickrApi(" and at the end ")" which needs to be cut off.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//you also need your own api key
private static final String FLICKRAPIKEY = "http://www.flickr.com/services/api/keys/ INSERT YOUR OWN API KEY";
 
private String flickrApi(String searchPattern, int limit) throws IOException, JSONException {
        URL url = new URL("http://api.flickr.com/services/rest/?method=flickr.photos.search&text=" + searchPattern + "&api_key=" + FLICKRAPIKEY + "&per_page="+ limit + "&format=json");
        URLConnection connection = url.openConnection();
        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
        while ((line = reader.readLine()) != null) {
                builder.append(line);
        }
 
        Log.d("not good",builder.toString());
        //no, this is not yet a valid json response :)
 
        int start = builder.toString().indexOf("(") + 1;
        int end = builder.toString().length() - 1;
        String jSONString = builder.toString().substring( start, end);
        //after cutting off the junk, its ok
 
        JSONObject jSONObject = new JSONObject(jSONString); //whole json object
        JSONObject jSONObjectInner = jSONObject.getJSONObject("photos"); //inner Json object
        JSONArray photoArray = jSONObjectInner.getJSONArray("photo"); // inner array of photos
        JSONObject photo = photoArray.getJSONObject((int) (limit*Math.random())); //get one random photo from array
 
        return constructFlickrImgUrl(photo, size._t);
}
 
// source: flickr.com/services/api/misc.urls.html
enum size {
        _s , _t ,_m
};
 
//helper method, to construct the url from the json object. You can define the size of the image that you want, with the size parameter. 
Be aware that not all images on flickr are available in all sizes.
private String constructFlickrImgUrl(JSONObject input, Enum size) throws JSONException {
        String FARMID = input.getString("farm");
        String SERVERID = input.getString("server");
        String SECRET = input.getString("secret");
        String ID = input.getString("id");
 
        StringBuilder sb = new StringBuilder();
 
        sb.append("http://farm");
        sb.append(FARMID);
        sb.append(".static.flickr.com/");
        sb.append(SERVERID);
        sb.append("/");
        sb.append(ID);
        sb.append("_");
        sb.append(SECRET);
        sb.append(size.toString());                    
        sb.append(".jpg");
 
        return sb.toString();
}
 
/**
 * Word randomizer for fun
 * @param length
 * @return
 */
private String randomizer(int length){
        char i[] = new char[length];
        for (int j = 0; j < length; j++) {
                i[j] =(char)((int)5*Math.random()+(int)'a');
        }
        return new String(i);
}
 
//Usage:
flickrApi(randomizer(3) , 1));

6 comments:

  1. give me full code.......

    ReplyDelete
  2. Hey Krishna, can you please send me the full source code for how to integrate the Flickr with android application, or how to upload the image from Flickr.? You can contact me on mansivora01@gmail.com. Thanks in advance.

    ReplyDelete
  3. we have Picasa application any idea share some little bit code explanation.

    ReplyDelete
  4. can you please give me code for android -flickr integration or how to uploade image from android application
    email id is-khajanpandey1990@yahoo.in
    Thank you

    ReplyDelete
  5. well its a complete copy of http://www.helloandroid.com/tutorials/flickr-api-random-image-goodness
    beware you are posting others content with your name

    ReplyDelete