1

I have the following code for cropping images. It's working perfectly in android version 4 or OS Kitkat, but its not working on Android version 5 or OS Lollipop.

I've already searched the whole world but couldn't find the answer...

Here's my code:

In OS Kitkat: this list variable return a value. but,
In OS Lollipop: this list variable return a empty arraylist.

    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 );
    PackageManager test = getPackageManager();

    int size = list.size();

    if (size == 0) {            

        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

        return;
    } else {
        intent.setData(mCapturedImageURI);

        intent.putExtra("outputX", 110);
        intent.putExtra("outputY", 110);
        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.appIntent= new Intent(intent);

                co.appIntent.setComponent( new ComponentName(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 (mCapturedImageURI != null ) {
                        getContentResolver().delete(mCapturedImageURI, null, null );
                        mCapturedImageURI = null;
                    }
                }
            } );

            AlertDialog alert = builder.create();

            alert.show();
        }
    }
nweg
  • 2,825
  • 3
  • 22
  • 30
  • Please disregard the PackageManager test = getPackageManager(); this is not included.. sorry i forgot to remove it upon posting.. – harry lingad Mar 18 '15 at 02:10
  • Can you please explain what the problem in in greater detail? What errors are you getting? – nweg Mar 18 '15 at 02:31
  • It doesnt return any error in logcat... It just assign a empty arraylist in the list variable (List).. So the list.size become 0 then it performs the Toast.makeText method then returns to the activity... My app doesnt crash but it doesnt crop either.. – harry lingad Mar 18 '15 at 04:38
  • If somebody has solutions about cropping on Lollipop, answer [here](http://stackoverflow.com/questions/31262080/always-null-returned-after-cropping-a-photo-from-a-uri-in-android-lollipop) please. – SilentKnight Jul 07 '15 at 07:50

1 Answers1

2

I had already given android.permission.MANAGE_DOCUMENTS permission. But facing the same issue. After searching a lot, i found the solution.

Here's a workaround, for the time being:

Intent mIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(mIntent, CHOOSE_IMAGE);

This forces the older image gallery to open instead of the new Kitkat documents view.

Now, you can get the Uri by calling the following in your onActivityResult:

Uri selectedImageURI = data.getData();

Hope this help to solve your problem.

Smeet
  • 4,036
  • 1
  • 36
  • 47