Thursday, 2 May 2013

Crop image in android

Sometimes when creating an Android app that includes user profile picture or avatar, we need to include a feature that enables users to select and crop image to update their profile picture. OnAndroid we can accomplish that by using intent to open image cropper app. To select an image from files, we can pass an intent to image gallery or file manager app then pass the selected image path to camera app to crop the image. It is also the same if we want to take a picture from camera, by passing an intent to camera app to open the camera, take a picture than save it to specified Uri then crop it.






main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Select and crop image"
        android:textSize="17sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_crop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select image" />

    <ImageView
        android:id="@+id/iv_photo"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

</LinearLayout>


crop_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:padding="10dp"
       android:gravity="center_vertical">
      
       <ImageView
              android:id="@+id/iv_icon"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
             
       <TextView
              android:id="@+id/tv_name"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text=""/>
</LinearLayout>


MainActivity:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.view.View;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.ImageView;

public class MainActivity extends Activity
{
       private Uri mImageCaptureUri;
       private ImageView mImageView;

       private static final int PICK_FROM_CAMERA = 1;
       private static final int CROP_FROM_CAMERA = 2;
       private static final int PICK_FROM_FILE = 3;

       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
              super.onCreate(savedInstanceState);

              setContentView(R.layout.main);

            final String [] items= new String [] {"Take from camera""Select from gallery"};                       
              ArrayAdapter<String> adapter= new ArrayAdapter<String> (this,       android.R.layout.select_dialog_item,items);
              AlertDialog.Builder builder= new AlertDialog.Builder(this);

              builder.setTitle("Select Image");
              builder.setAdapter( adapter, new DialogInterface.OnClickListener()
              {
                     public void onClick( DialogInterface dialog, int item )
                     {
                           //pick from camera
                           if (item == 0)
                           {
                                  Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                           mImageCaptureUri = Uri.fromFile(newFile(Environment.getExternalStorageDirectory(),
                     "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

                 intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUTmImageCaptureUri);

                                  try
                                  {
                                         intent.putExtra("return-data"true);

                                         startActivityForResult(intent, PICK_FROM_CAMERA);
                                  }
                                  catch (ActivityNotFoundException e)
                                  {
                                         e.printStackTrace();
                                  }
                           }
                           else
                           { //pick from file
                                  Intent intent = new Intent(); 
                                  intent.setType("image/*");
                                  intent.setAction(Intent.ACTION_GET_CONTENT);

           startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
                           }
                     }
              } );

              final AlertDialog dialog = builder.create();

              Button button= (Button) findViewById(R.id.btn_crop);
              mImageView = (ImageView) findViewById(R.id.iv_photo);

              button.setOnClickListener(new View.OnClickListener()
              {     
                     @Override
                     public void onClick(View v)
                     {
                           dialog.show();
                     }
              });
       }

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data)
       {
              if (resultCode != RESULT_OKreturn;

              switch (requestCode)
              {
              case PICK_FROM_CAMERA:
                     doCrop();

                     break;

              case PICK_FROM_FILE:
                     mImageCaptureUri = data.getData();
                     doCrop();

                     break;       

              case CROP_FROM_CAMERA:           
                     Bundle extras = data.getExtras();

                     if (extras != null)
                     {                   
                           Bitmap photo = extras.getParcelable("data");

                           mImageView.setImageBitmap(photo);
                     }

                     File f = new File(mImageCaptureUri.getPath());           

                     if (f.exists()) f.delete();

                     break;

              }
       }

       private void doCrop()
       {
              final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

              Intent intent = new Intent("com.android.camera.action.CROP");
              intent.setType("image/*");

              List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

              int size = list.size();

              if (size == 0)
              {              
                     Toast.makeText(this"Can not find image crop app", Toast.LENGTH_SHORT).show();
                     return;
              }
              else
              {
                     intent.setData(mImageCaptureUri);

                     intent.putExtra("outputX", 200);
                     intent.putExtra("outputY", 200);
                     intent.putExtra("aspectX", 1);
                     intent.putExtra("aspectY", 1);
                     intent.putExtra("scale"true);
                     intent.putExtra("return-data"true);

                     if (size == 1) {
                           Intent i = new Intent(intent);
                           ResolveInfo res= list.get(0);

               i.setComponent( new ComponentName(res.activityInfo.packageName,  res.activityInfo.name));

                       startActivityForResult(i, CROP_FROM_CAMERA);
                     }
                     else
                     {
                           for (ResolveInfo res : list)
                           {
                                  final CropOption co = new CropOption();

                                  co.title      = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                                  co.icon              = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                                  co.appIntentnew Intent(intent);

                                  co.appIntent.setComponent( newComponentName(res.activityInfo.packageName, res.activityInfo.name));

                                  cropOptions.add(co);
                           }

                           CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

                           AlertDialog.Builder builder = new AlertDialog.Builder(this);
                           builder.setTitle("Choose Crop App");
                            builder.setAdapter( adapter, new DialogInterface.OnClickListener()
                           {
                                  public void onClick( DialogInterface dialog, int item )
                                  {
                                         startActivityForResult( cropOptions.get(item).appIntent,CROP_FROM_CAMERA);
                                  }
                           });

                           builder.setOnCancelListener( new DialogInterface.OnCancelListener()
                           {
                                  @Override
                                  public void onCancel( DialogInterface dialog )
                                  {

                                         if (mImageCaptureUri != null )
                                         {
                                                getContentResolver().delete(mImageCaptureUrinullnull);
                                                mImageCaptureUri = null;
                                         }
                                  }
                           } );

                           AlertDialog alert = builder.create();

                           alert.show();
                     }
              }
       }
}

CropOptionAdapter:

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.Context;
import java.util.ArrayList; 

public class CropOptionAdapter extends ArrayAdapter<CropOption>
{
       private ArrayList<CropOption> mOptions;
       private LayoutInflater mInflater;
      
       public CropOptionAdapter(Context context, ArrayList<CropOption> options)
       {
              super(context, R.layout.crop_selector, options);
             
              mOptions= options;        
              mInflater= LayoutInflater.from(context);
       }
      
       @Override
       public View getView(int position, View convertView, ViewGroup group)
       {
              if (convertView == null)
                     convertView = mInflater.inflate(R.layout.crop_selectornull);
             
              CropOption item = mOptions.get(position);
             
              if (item != null)
              {
                     ((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
                     ((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
                    
                     return convertView;
              }
             
              return null;
       }
}


CropOption:

import android.content.Intent;
import android.graphics.drawable.Drawable;

public class CropOption
{
       public CharSequence title;
       public Drawable icon;
       public Intent appIntent;
}


Download Source Code: