Orkut Application Platform

The orkut Client Library - Photos

Bruno Oliveira - Developer Relations
December 2010

This section of the guide explains how to perform common operations involving photos using the orkut Client Library. If you haven't read it yet, please read the Introduction to the Client Library first, since we'll assume you know how to perform the library and environment setup.

Contents:

Since this section explains how to manage photos, it also requires you to understand first how to manage albums. In order to read more about photo albums, please refer to the Album Transactions section of this guide.

Listing Photos in an Album

Photos are always in albums, so in order to list photos, you must already have the ID of the album you want to list. If you don't, you'll probably want to list the albums first.

Once you have the ID of the album you want to list, you must create a GetPhotosTx by using either the getSelfPhotos() or the getPhotosOf() function of the photo transaction factory (PhotoTxFactory) provided by your OrkutAdapter.

For example, to list the first 20 photos in a given album, here is what you could do:

GetPhotosTx tx = orkad.getPhotosTF().getSelfPhotos(albumId);
BatchTransaction btx = orkad.newBatch();
tx.setCount(20); // get up to 20 photos
btx.add(tx);
say("Getting photos...");
orkad.submitBatch(btx);

If you don't call tx.setCount(20), a default value will be used. However, if you want control over how many photos will be returned, you should call it. After obtaining the result, you can list the actual photos by means of the tx.getPhotoCount() and tx.getPhoto(i). The latter function returns a Photo object.

The Photo object represents a single photo and has the following accessor functions:

  • getId - returns the ID of the photo.
  • getTitle - returns the title of the photo.
  • getUrl - returns the URL of the photo. You can obtain the photo from this URL directly without using OAuth. So launching a browser with this URL should work, as well as setting this URL as the src attribute of an img tag on a page.
  • getThumbnailUrl - returns the URL from which a thumbnail of the photo can be downloaded.

So, to complete the example:

GetPhotosTx tx = orkad.getPhotosTF().getSelfPhotos(albumId);
BatchTransaction btx = orkad.newBatch();
tx.setCount(20); // get up to 20 photos
btx.add(tx);
orkad.submitBatch(btx);

if (tx.hasError()) { say("*** Error getting photos."); return; }

int i;
for (i = 0; i < tx.getPhotoCount(); i++) {
   Photo p = tx.getPhoto(i);
   say("Photo ID        : " + p.getId());
   say("Photo Title     : " + p.getTitle());
   say("Photo URL       : " + p.getUrl());
   say("Thumbnail URL   : " + p.getThumbnailUrl());
   say("");
}

In order to get someone else's photos, all you have to do is replace getSelfPhotos by getPhotos(userId,albumId):

GetPhotosTx tx = orkad.getPhotosTF().getPhotos(userId,albumId);

Updating Photo Information

Updating a photo's title can be accomplished by first obtaining the Photo object as described above, then setting its title with setTitle(), then submitting that back using a UpdatePhotoTx:

Photo photo;

// (...get photo here...)

photo.setTitle("new title");
btx = orkad.newBatch();
UpdateAlbumTx utx = orkad.getPhotosTF().updatePhoto(photo);
btx.add(utx);
orkad.submitBatch(btx);

Deleting a Photo

To delete a photo, you must first get the Photo object, and then simply use a DeletePhotoTx transaction in this manner:

Photo photo;

// (...get photo here...)

btx = orkad.newBatch();
DeletePhotoTx utx = orkad.getPhotosTF().deletePhoto(photo);
btx.add(utx);
orkad.submitBatch(btx);

Uploading a Photo

Lastly, you can also upload a new photo to an album. There are two ways to do this: you can either supply the JPEG data directly as a byte array (byte[]) or you can simply supply the name of the file that holds the photo (in JPEG format).

Here is an example where we upload a photo that's stored as "myphoto.jpg" in the current directory, with a title of "My Photo Title", to an album whose ID we have in the albumId variable:

UploadPhotoTx tx = orkad.getPhotosTF().uploadPhoto(
        albumId,"myphoto.jpg","My Photo Title");
BatchTransaction btx = orkad.newBatch();
btx.add(tx);
say("Submitting photo...");
orkad.submitBatch(btx);

if (tx.hasError())
   say("*** Error uploading photo.");

If, instead, you had the photo as a byte[] array, you could instead use:

byte[] buf;

// (...get the image from somewhere...)

UploadPhotoTx tx = orkad.getPhotosTF().uploadPhoto(
        albumId,buf,ImageType.JPG,"My Photo Title");

Notice that with this second form, you must specify the image's file type (ImageType.JPG) in order to correctly identify it as a JPEG. Other formats for uploading are supported, but not recommended.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.