Sample code for 30+ languages & platforms
Java

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 Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    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) {
        System.out.println(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) {
        System.out.println(rest.lastErrorText());
        return;
        }

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

    // Show the JSON in human-readable format.
    System.out.println(json.emit());

    // Get the image URL.
    String imageUrl = json.stringOf("images[0].source");
    System.out.println("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");
        }

    System.out.println("Downloading to " + sbToPath.getAsString());

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