1

I resolved the issue , here is the updated code with the help of (android:select image from gallery then crop that and show in an imageview post in stackoverflow(Thanks to Dhaval Patel and atifali)

my activity class:

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;
String filePath;
ImageView bitmapView;
private final int RESULT_CROP = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
FrameLayout wal = (FrameLayout) findViewById(R.id.fm);
wal.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {

            try{
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE); 
        }catch(Exception e){
            e.printStackTrace();
        }}
    });
}
        @Override  
        public void onActivityResult(int requestCode, int resultCode, Intent  data) {  

             super.onActivityResult(requestCode, resultCode, data);  
             if (requestCode == RESULT_LOAD_IMAGE && resultCode ==RESULT_OK && null != data) {
                 Uri picUri = data.getData();
                 String[] filePathColumn = {MediaStore.Images.Media.DATA};
                 Cursor cursor = getContentResolver().query(picUri,
                         filePathColumn, null, null, null);
                 cursor.moveToFirst();
                 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                 filePath = cursor.getString(columnIndex);
                 cursor.close();
                 doCrop(filePath);

                 if (requestCode == RESULT_CROP ) {
                     if(resultCode == Activity.RESULT_OK){  
                   Bundle extras = data.getExtras();
                 Bitmap selectedBitmap = extras.getParcelable("data");
                         // Set The Bitmap Data To ImageView
                 bitmapview.setImageBitmap(selectedBitmap);                             
                 bitmapview.setScaleType(ScaleType.FIT_XY);
                     }
                 }
                }
            }

            private void doCrop(String picPath) {
                try {


                    Intent cropIntent = new Intent("com.android.camera.action.CROP");

                    File f = new File(picPath);
                    Uri contentUri = Uri.fromFile(f);

                    cropIntent.setDataAndType(contentUri, "image/*");

                    cropIntent.putExtra("crop", "true");
                    // indicate aspect of desired crop
                    cropIntent.putExtra("aspectX", 1);
                    cropIntent.putExtra("aspectY", 1);
                    // indicate output X and Y
                    cropIntent.putExtra("outputX", 280);
                    cropIntent.putExtra("outputY", 280);

                    // retrieve data on return
                    cropIntent.putExtra("return-data", true);
                    // start the activity - we handle returning in onActivityResult
                    startActivityForResult(cropIntent, RESULT_CROP);
                }
                catch (ActivityNotFoundException anfe) {
                    String errorMessage = "your device doesn't support the crop action!";
                    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
                    toast.show();
                }
            }   

}

Community
  • 1
  • 1
Adithya
  • 183
  • 1
  • 2
  • 16
  • 1
    Please specify the problem you face. – atifali Oct 20 '15 at 06:56
  • Hi Atifail, Actually the image is getting cropped but the imageview's background is reflecting with original centercrop image instead.I mean ones I pick the image from gallery and next cropping it and when I click on save this cropped image is not reflecting the original image from gallery only sitting in imageview. – Adithya Oct 20 '15 at 08:18

1 Answers1

0

Basically you want to change aspect ratio in crop rectangle.For this, just comment out following lines in your code.

cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
atifali
  • 193
  • 16
  • Hi Atifail, Actually the image is getting cropped but the imageview's background is reflecting with original centercrop image instead.I mean ones I pick the image from gallery and next cropping it and when I click on save this cropped image is not reflecting the original image from gallery only sitting in imageview. – Adithya Oct 20 '15 at 08:11
  • Whether you mean the cropped image is not being set to imageView or imageview shows the both original and cropped image.??? – atifali Oct 21 '15 at 05:34
  • I resolved the issue , actually I forgot to get the picture path string via intent .I am adding the code snippet for others. – Adithya Oct 21 '15 at 12:24