Download Code

Android Gestures Image Gallery

In this tutorial you will learn how to pick an image from android phone gallery.Apply zoom in/out gestures on that image.Let’s talk breifly about our concerning classes.

Intent

An intent is an abstract description of an operation to be performed.In this topic you will ACTION_PICK constant of an intent.

MediaStore

The Media provider contains meta data for all available media on both internal and external storage devices.

Cursor

This interface provides random read-write access to the result set returned by a database query.

Gestures

There are different types of Gestures like Multi Touch,Tracking Movement etc.But you will use Touch Gesture.

A “touch gesture” occurs when a user places one or more fingers on the touch screen.Your application interprets that pattern of touches as a particular gesture.

Let’s start create new android application project.Go to activity_main.xml and add ImageView,Button.

But one thing you should keep in mind that your layout should RelativeLayout.Why,because it expand according to the gesture.And also set ImageView scaleType to Matrix.

activity_main.xml

imageview,image from gallery android, pick image from gallery,how to pick image from gallery android tutorial, pick image android gallery,how to pick image from gallery and display in imageview android tutorial, image picker android tutorial,Image picker Intent,Intent to pick image, Intent to select image from gallery android tutorial, image pick from gallery android source code, Gesture,gestures,gesture in android tutorial,gesture source code, ScaleGestureDetector,SimpleOnScaleGestureListener,Matrix, how to zoom in/out image in android,bitmap, bitmapfactory,how to decode image file to bitmap in android tutorial, onActivityResult,startActivityforResult,MediaStore,cursor,how to get image using cursor,

Now go to MainActivity.java.Do the following steps.

  • Create variables and refer UI Components(ImageView,Button).
  • Apply click event on Button.
  • OnButton click start intent to pick Image from gallery with onActivityResult().
  • Create a class (Zoomer) and extend with ScaleGestureDetector.
  • (Zoomer class will rescale your image or any other components).

MainActivity.java

package developer.codecpickergesture;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener{
	Button pickImageBtn;
	ImageView pickedImage;
	Bitmap bitImage;
	ScaleGestureDetector detector;
	private Matrix matrix = new Matrix();
	private float scale = 1f;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialize();
		
	}
	public void initialize(){
		pickedImage = (ImageView) findViewById(R.id.image);
		pickImageBtn = (Button) findViewById(R.id.pickImage);
		pickImageBtn.setOnClickListener(this);
		detector = new ScaleGestureDetector(this,new Zoomer());
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.pickImage:
                          //image from gallery intent
			Intent p = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(p, 1);
			break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			pickedImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));

		}

	}
	 public boolean onTouchEvent(MotionEvent ev) {
	      detector.onTouchEvent(ev);
	      return true;
	   }

	private class Zoomer extends ScaleGestureDetector.SimpleOnScaleGestureListener{

		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			scale *= detector.getScaleFactor();
	         scale = Math.max(0.1f, Math.min(scale, 5.0f));
	         
	         matrix.setScale(scale, scale);
	         pickedImage.setImageMatrix(matrix);
	         return true;
		}

		
	}


}

Go to Android Manifest.xml file and add permission EXTERNAL_STORAGE.

imageview,image from gallery android, pick image from gallery,how to pick image from gallery android tutorial, pick image android gallery,how to pick image from gallery and display in imageview android tutorial, image picker android tutorial,Image picker Intent,Intent to pick image, Intent to select image from gallery android tutorial, image pick from gallery android source code, Gesture,gestures,gesture in android tutorial,gesture source code, ScaleGestureDetector,SimpleOnScaleGestureListener,Matrix, how to zoom in/out image in android,bitmap, bitmapfactory,how to decode image file to bitmap in android tutorial, onActivityResult,startActivityforResult,MediaStore,cursor,how to get image using cursor,

Run your android application project.

imageview,image from gallery android, pick image from gallery,how to pick image from gallery android tutorial, pick image android gallery,how to pick image from gallery and display in imageview android tutorial, image picker android tutorial,Image picker Intent,Intent to pick image, Intent to select image from gallery android tutorial, image pick from gallery android source code, Gesture,gestures,gesture in android tutorial,gesture source code, ScaleGestureDetector,SimpleOnScaleGestureListener,Matrix, how to zoom in/out image in android,bitmap, bitmapfactory,how to decode image file to bitmap in android tutorial, onActivityResult,startActivityforResult,MediaStore,cursor,how to get image using cursor,

That’s it for this tutorial.

Thanks.