Search code examples
androidandroid-intentandroid-4.4-kitkatandroid-camera-intent

android KitKat image Crop


I am passing image url to the following method

private void performCrop(Uri imageUri){
        try {
            Intent intent = new Intent("com.android.camera.action.CROP"); 
          //  intent.setType("image/*");
            intent.setDataAndType(imageUri, "image/*");
            List<ResolveInfo> list = getActivity().getPackageManager().queryIntentActivities( intent, 0 );
            int size = list.size();

            if (size >= 0) {
                intent.setData(imageUri);        
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 0);
                intent.putExtra("aspectY", 0);
                intent.putExtra("outputX", 150);
                intent.putExtra("outputY", 150);
                intent.putExtra("scale", true);
                intent.putExtra("scaleUpIfNeeded", true);
                intent.putExtra("return-data", true);
                Intent i = new Intent(intent);

                ResolveInfo res = list.get(0);

                i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                System.out.println("before startActivityForResult");
                try{
                startActivityForResult(i, 2);  
                }catch(Exception e){
                    e.printStackTrace();
                }
             } 

        }
        catch(ActivityNotFoundException anfe){
            String errorMessage = "Whoops - your device doesn't support the crop action!";
            //Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            //toast.show();
        }
    }

some of images are cropped but some image are not. When I crop image then my app's activity is closed and application starts it's previous or launcher activity so what is the problem??

the main issue is I am not getting any warning or error in log cat, and when I debug it the the till "System.out.println("before startActivityForResult");" the program excutes, that means it does not goes in or calling onActivityResult().

here is onActivityResult method

 @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
      // TODO Auto-generated method stub
      super.onActivityResult(requestCode, resultCode, data);




if(requestCode ==2 && resultCode == getActivity().RESULT_OK){
            System.out.println("inside logic...");
            try{
                if (data != null) {

               //  get the returned data
                    Bundle extras = data.getExtras();
                    // get the cropped bitmap
                    Bitmap bmp = extras.getParcelable("data");

                        imageView.setImageBitmap(bmp);


                }   
            }catch(Exception e){
                e.printStackTrace();
            }

     }
    }

Solution

  • Android all devices does not have a cropping intent according @CommonsWare http://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html

    so better is to use libraries

    some of them are:

    https://github.com/jdamcd/android-crop

    https://github.com/IsseiAoki/SimpleCropView

    https://android-arsenal.com/details/1/3054