0

I'm working on an app which allows user to choose a picture from gallery and then I start a activity to crop it.

I want to send the cropped image back to calling activity.

Both activities extend AppCompatActivity.

Calling activity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {    

            // start image crop activity
            String dataString = data.getDataString();    
            Intent intent=new Intent(this, CropPhotoActivity.class);
            intent.putExtra("SELECTED_PICTURE_FOR_CROP", dataString);
            startActivityForResult(intent, CROP_PICTURE);    
        }
        else if(requestCode == CROP_PICTURE) {
            // get cropped bitmap
            Bitmap bitmap = (Bitmap) data.getParcelableExtra("CROPPED_IMAGE");
            profilePhoto.setImageBitmap(bitmap);
        }
    }
}

In the crop image activity, I have a button, which on click should return back to calling activity:

Button okButton = (Button)findViewById(R.id.ok_button);
okButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent returnIntent = new Intent();
        returnIntent.putExtra("CROPPED_IMAGE", cropped_bitmap);
        setResult(RESULT_OK, returnIntent);
        finish(); // sometimes restarts app
    }
});

Sometimes the bitmap gets returned correctly whereas sometimes it does not and the app gets restarted without error. Why is this happening? Does putExtra have anything to do with bitmap size or anything else?

user5155835
  • 4,392
  • 4
  • 53
  • 97

2 Answers2

0

You could try substituting

AppcompatActivity.this.finish() 

(where AppcompatActivity is your class name)

for:

finish(); // sometimes restarts app

Or, create a method in the calling Activity:

public static void cropComplete(Activity activity)
{
    activity.startActivity(activity, AnotherActivity.class);
    activity.finish();
}
a person
  • 986
  • 6
  • 13
0

Theres's a limit for data length passed as extra in a intent. Try not passing the dataString value; instead you should save the image as a temporary file, pass the path in the intent and then load the image from your calling activity (or you can just save the dataString in a static helper class).

In the crop activity (saving bitmap code from Save bitmap to location):

// Save bitmap
String filename = "tempImage.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);
FileOutputStream out = null;
try {
    out = new FileOutputStream(dest);
    bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (out != null)out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// Get image file path
String path = dest.getAbsolutePath();

// Set result with image path
Intent returnIntent = new Intent();
returnIntent.putExtra("CROPPED_IMAGE_PATH", path);
setResult(RESULT_OK, returnIntent);
finish();

In the caller activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
    if(requestCode == CROP_PICTURE) {
        // Get image file path
        String imagePath = data.getStringExtra("CROPPED_IMAGE_PATH");

        // Load image
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
    }
}
Community
  • 1
  • 1