Sample code for 30+ languages & platforms
Android™

Download Photo to a File

See more Facebook Examples

Assuming we have the ID of a Photo, this example demonstrates how to download the photo image data to a file.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example assumes a previously obtained an access token
    CkOAuth2 oauth2 = new CkOAuth2();
    oauth2.put_AccessToken("FACEBOOK-ACCESS-TOKEN");

    CkRest rest = new CkRest();

    // Connect to Facebook...
    success = rest.Connect("graph.facebook.com",443,true,true);
    if (success != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    // Provide the authentication credentials (i.e. the access key)
    rest.SetAuthOAuth2(oauth2);

    // Assumes we've already obtained a Photo ID.
    String photoId = "10210199026347451";

    CkStringBuilder sbPath = new CkStringBuilder();
    sbPath.Append("/v2.7/");
    sbPath.Append(photoId);

    // First we're going to get the photo informaton so we can get the URL of the image file data.
    // Select the fields we want.
    // See https://developers.facebook.com/docs/graph-api/reference/photo/
    rest.AddQueryParam("fields","id,album,images");

    String responseJson = rest.fullRequestNoBody("GET",sbPath.getAsString());
    if (rest.get_LastMethodSuccess() != true) {
        Log.i(TAG, rest.lastErrorText());
        return;
        }

    CkJsonObject json = new CkJsonObject();
    json.put_EmitCompact(false);
    json.Load(responseJson);

    // Show the JSON in human-readable format.
    Log.i(TAG, json.emit());

    // Get the image URL.
    String imageUrl = json.stringOf("images[0].source");
    Log.i(TAG, "Downloading from " + imageUrl);

    CkStringBuilder sbImageUrl = new CkStringBuilder();
    sbImageUrl.Append(imageUrl);

    // Build the output local file path.
    CkStringBuilder sbToPath = new CkStringBuilder();
    sbToPath.Append("qa_output/fb");
    sbToPath.Append(json.stringOf("id"));
    boolean bCaseSensitive = false;
    if (sbImageUrl.Contains(".jpg",bCaseSensitive) == true) {
        sbToPath.Append(".jpg");
        }
    else {
        sbToPath.Append(".png");
        }

    Log.i(TAG, "Downloading to " + sbToPath.getAsString());

    // Download using Chilkat HTTP.
    CkHttp http = new CkHttp();
    success = http.Download(imageUrl,sbToPath.getAsString());
    if (success != true) {
        Log.i(TAG, http.lastErrorText());
        }
    else {
        Log.i(TAG, "Downloaded.");
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}