b2cloud

6th September 2012

Instagram integration in an Android app Part – 2

Guides | Tutorial By 4 years ago

I hope you guys enjoyed my previous blog about Instagram Integration.

Here is part 2 in the series.

Before we start, I am assuming that you have been through my previous blog because i am reusing  some of the content here. In this tutorial i will explain how to get the user’s photographs from the Instagram.

To get user’s photos we don’t actually need any scope permission. It is already included in a basic scope type that is a default scope set by Instagram.

Like previous tutorial we need to form an endpoint URL to hit the server for photos request: –

String urlString = APIURL + "/users/"+ {User's Instagram Id} +"/media/recent/?access_token=" + {Instagram Access Toke};
URL url = new URL(urlString);

Two things to notice in above urlString: – Instagram Id and Access Token. We already have Instagram Access token and User Id if you have go through my previous tutorial.

Now open a connection and get the input stream from the url. You need to convert the input stream into a string before start using it. I’ll share the streamToString() function code later in this tutorial.

InputStream inputStream = url.openConnection().getInputStream();
String response = instagramImpl.streamToString(inputStream);

Instagram sends response in the form of Json so we need to convert the response into Json.

JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue();
JSONArray jsonArray = jsonObject.getJSONArray("data");

And now this is how you’ll get the image url: –

JSONObject mainImageJsonObject = jsonArray.getJSONObject(index).getJSONObject("images").getJSONObject("low_resolution");//Use for loop to traverse through the JsonArray.
String imageUrlString = imageJsonObject.getString("url");

Instagram saves images in three different size on its server so we have three options to choose from: –

1. Standard Resolution – standard_resolution
2. Low Resolution – low_resolution
3. Thumbnail – thumbnail

In my code i used low_resolution.

And now make sure you put all this code in an AsyncTask.

And as i promised here is the code for streamToString() method: –

public String streamToString(InputStream is) throws IOException {
String string = "";

	if (is != null) {
		StringBuilder stringBuilder = new StringBuilder();
		String line;
		try {
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));

			while ((line = reader.readLine()) != null) {
				stringBuilder.append(line);
			}

			reader.close();
		} finally {
			is.close();
		}

		string = stringBuilder.toString();
	}

	return string;
}

So this the end of part -2. I hope you enjoyed it.

Thanks for reading.

  • Mihir

    How do I load the image from imageUrlString in an activity from the web view client

  • Your code and explanation are pretty clear and brilliant. Thank you.

    • varun_b2cloud

      Thanks 🙂

  • Akhilesh Mani Tiwari

    Hi Varun, nice work. Is there any way to post image in Instagram just like FB.

    • varun_b2cloud

      Hi Akhilesh, Unfortunately the answer is No. Instagram APIs do not give you the power to upload images.

  • caiser

    Hi Varun,
    Can we please have a sample project code for this ?
    I can’t get it to work
    Thanks

    • varun_b2cloud

      Hey Caiser, Check out the part – 1, i shared the demo code in that blog.
      Cheers!!

      • caiser

        I have the part-1 project dear, however it doesn’t have this image retrieving example.

        The line “JSONArray jsonArray = jsonObject.getJSONArray(“data”);” crashers the app.

        • varun_b2cloud

          It might be because the Json you received doesn’t have the JSONArray called “data”. I can’t tell you the right reason for that as i don’t have an access to your code but i can suggest you few tips like make sure you are using the right client id and secret and try to log the urlString and test it manually. You can test the URLs here “http://instagram.com/developer/api-console/” to make sure that its a right endpoint to hit.

          • caiser

            Yeah I fixed the problem few days ago.

            Your URL is wrong. It should be something like this “https://api.instagram.com/v1/users/self/feed?access_token=”.

            you have also some errors in the codoe like the variable “imageJsonObject” that should be mainImageJsonObject

  • varun_b2cloud

    Hi caiser, i have tested this code before publishing this tutorial. But as it is more than a year old, things might have changed. Still thanks for mentioning it. It might help other people struggling with same issue. Thanks

  • Partha Chakraborty

    hi varun! could you integrate this second tutorial with the first and post the whole project? Thanks in advance 🙂

Recommended Posts

Instagram integration in an Android app.

Post by 4 years ago

Instagram Integration in Android Part – 1 Instagram is a new but successful player in social domain nowadays. I am pretty sure many Android developers out there would love to integrate this cool social platform

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!