Sep 12 2010   9:58 pm

Browsing Image Gallery With PhoneGap & Android

I am porting an application I wrote for the iphone over to Android and I wanted to start with the Camera functionality. I looked around and Googled and I could not find where it was documented on how to browse the image gallery on the Android device. I decided to bang out some simple modifications to the existing Camera object to support that functionality for now. I the future, hopefully this is implemented better by the PhoneGap guys and I can put this code out to pasture.

This code needs to be added to the phoneGap.js file. It creates the prototype for the new method for browsing the gallery on the device

//
// add the new prototype to support the browse function
//
// i have created a new class 'PixCameraLauncher' so I did not have to modify
// the original class file provided by PhoneGap
//
com.phonegap.CameraLauncherProxy.prototype.browseImages = function(quality) {
    return PhoneGap.exec("com.phonegap.gapTest.PixCameraLauncher", "browseImages", [quality]);
};


This the modification I made to the CameraLauncher class provided by PhoneGap. Add a new intent to launch the image gallery

	/**
	 * Get a picture from the Image Gallery.
	 * 
	 * in DroidGap.onActivityResult, which forwards the result to
	 * this.onActivityResult.
	 * 
	 * @param quality
	 *            Compression quality hint (0-100: 0=low quality & high
	 *            compression, 100=compress of max quality)
	 */
	public void browseImages(int quality) {
		this.mQuality = quality;

		// Display camera
		Intent intent = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

		this.ctx.startActivityForResult((Plugin) this, intent);
	}


Also we need to handle the returned file. I left it simple, but this could be cleaned up a bit What I do is assume that if there is a URI in the data field, then an image has been picked and should be processed by the utility. In the beginning of the

void onActivityResult(int requestCode, int resultCode, Intent intent)

add the code below. This will handle re-assigning the URI to the selected file

		// If image available
		if (resultCode == Activity.RESULT_OK) {

			//
			// this means the user selected an image, so just pass it back!!
			//
			if (intent.getData() != null) {
				imageUri = intent.getData();
			}



After a little more testing and code cleanup, I will make the complete project for the android work and the iphone work available. If there are any questions, post it below.