SpyderHunter03
SpyderHunter03

Reputation: 63

Android Full Image From Crop Intent

I am taking images using the camera on an Android phone. After the image is taken, I pass the full image as a uri to the crop intent (com.android.camera.action.CROP). However, when the crop intent comes back, it returns in the parcelable at thumbnail size. I have tried to save the image from the camera in a file and send the uri of the file instead but it doesn't open the image in the crop intent. How can I get the full size image that I cropped in the crop intent from the parcel it sends back? Here is some of the code I am using:

public void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    if (resultCode != Activity.RESULT_CANCELED) {
        Uri uri = null;
        switch (requestCode) {
        case FILE_SELECT_CODE:
            if (intent.getData() != null) {
                uri = intent.getData();
                cropSelectedPicture(uri);
            }
            break;
        case PICTURE_SELECT_CODE:
            uri = intent.getData();
            if (uri == null) {
                Bundle bundle = intent.getExtras();
                Bitmap photo = null;
                if (bundle != null) {
                    photo = bundle.getParcelable("data");
                }
                finishChangeProfilePicture(photo);
            } else {
                cropSelectedPicture(uri);
            }
            break;
        case FILE_CROP_CODE:
            Bundle extras = intent.getExtras();
            Bitmap thePic = extras.getParcelable("data");
            uri = extras.getParcelable("uri");
            finishChangeProfilePicture(thePic);
            break;
        }
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

public void showCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra("crop", "true");
    intent.putExtra("outputX", pictureWidth);
    intent.putExtra("outputY", pictureHeight);
    intent.putExtra("aspectX", pictureWidth);
    intent.putExtra("aspectY", pictureHeight);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);
    try {
        cordova.startActivityForResult(
                this, Intent.createChooser(intent, "Take Picture"),
                PICTURE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(context, "Please install a Camera application.", 
                Toast.LENGTH_SHORT).show();
    }
}

public void cropSelectedPicture(Uri uri) {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("outputX", pictureWidth);
    intent.putExtra("outputY", pictureHeight);
    intent.putExtra("aspectX", pictureWidth);
    intent.putExtra("aspectY", pictureHeight);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);

    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
    if (activities.size() > 0) {
        for (int i = 0; i < activities.size(); i++) {
            String label = (String) activities.get(i).loadLabel(packageManager);
            if (label.equals("Crop picture")) {
                ActivityInfo activity = activities.get(i).activityInfo;
                ComponentName name = new ComponentName (activity.applicationInfo.packageName,
                                                        activity.name);
                intent.setComponent(name);
            }
        }
    }

    try {
        cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 0

Views: 2022

Answers (1)

SpyderHunter03
SpyderHunter03

Reputation: 63

Okay, so what I had to do was save the uri from the camera intent and then save the image to that intent when coming out of the crop intent. I had tried this once before but it was to an internal file to the app instead of to the content resolver. (Fair warning: This is a cordova project, not a native android project, so if you are having trouble with the Media.getBitmap line, then remove the cordova.getActivity() part and that should make it work) Here is what I changed:

public void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    if (resultCode != Activity.RESULT_CANCELED) {
        switch (requestCode) {
        case FILE_SELECT_CODE:
            if (intent.getData() != null) {
                uri = intent.getData();
                cropSelectedPicture();
            }
            break;
        case PICTURE_SELECT_CODE:
            uri = intent.getData();
            if (uri == null) {
                Bundle bundle = intent.getExtras();
                Bitmap photo = null;
                if (bundle != null) {
                    photo = bundle.getParcelable("data");
                }
                finishChangeProfilePicture(photo);
            } else {
                cropSelectedPicture();
            }
            break;
        case FILE_CROP_CODE:
            Bitmap theUriPic;
            try {
                theUriPic = Media.getBitmap(cordova.getActivity().getContentResolver(), uri);
                finishChangeProfilePicture(theUriPic);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }
    super.onActivityResult(requestCode, resultCode, intent);
}

public void cropSelectedPicture() {
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(uri, "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("outputX", pictureWidth);
    intent.putExtra("outputY", pictureHeight);
    intent.putExtra("aspectX", pictureWidth);
    intent.putExtra("aspectY", pictureHeight);
    intent.putExtra("scale", true);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    intent.putExtra("return-data", true);

    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
    if (activities.size() > 0) {
        for (int i = 0; i < activities.size(); i++) {
            String label = (String) activities.get(i).loadLabel(packageManager);
            if (label.equals("Crop picture")) {
                ActivityInfo activity = activities.get(i).activityInfo;
                ComponentName name = new ComponentName (activity.applicationInfo.packageName,
                                                        activity.name);
                intent.setComponent(name);
            }
        }
    }

    try {
        cordova.startActivityForResult(this, intent, FILE_CROP_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(context, "Please install a Cropping program", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 1

Related Questions