Sica
Sica

Reputation: 79

How to start a new activity passing an image uri

I have an activity with a button that opens a dialog allowing to choose between Taking a picture or Selecting a picture from gallery coded like that:

private void showDialogToSelectAFile() {
    MaterialAlertDialogBuilder dialog = new MaterialAlertDialogBuilder(this);

    dialog.setTitle("Select a file or take a picture")
            .setMessage("Choose one of the options")
            .setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //do nothing
                }
            }).setPositiveButton("Take a picture", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            openCameraToTakePictureIntent();
        }
    }).setNegativeButton("Select a file from your gallery", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            openGalleryIntent();
        }
    }).show();
}

Then it runs the following intent to do the selected action:

private void openCameraToTakePictureIntent() {
    Log.d(TAG, "Method for Intent Camera started");
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.emergence.domain.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

}

Now I want to start a new activity that will display the picture and allow modifications on this picture so I though I'd pass the URI in an "onActivityResult" but I can't figure out how to do it.

So far it doesn't crash, I can select an image or take a picture but of course when I do so, it only goes back to the activty doing nothing except hopefully storing the image in the file.

My questions: Is there a better way to proceed to pass an image to a new activity while keeping it full quality ? If not, how can I pass this URI and retrieve the image in the new activity ? Is my image actually stored in the file I created ?

Here is my createImageFile function in case it's relevant:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

Thanks for your help

EDIT:

I have updated the onActivityResult as follow:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK && requestCode == 1) {

        Intent intent = new Intent(this, ModifyPictureActivity.class);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, currentPhotoPath);
        startActivity(intent);

}

I created this new activity that only has an ImageView in which I want to display the picture but I can't find the right method to set the image to the Extras. So far I have this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modify_picture);

    Intent intent = getIntent();
    intent.getExtras();

    image = findViewById(R.id.image);

    image.setImageURI(???);

}

Should I try to set the image with another method than "setImageURI"?

Upvotes: 1

Views: 223

Answers (2)

Róbert Nagy
Róbert Nagy

Reputation: 7602

Actually you have two options:

  1. If you want a small-sized image, you can get it in the onActivityResult callback via decoding the bitmap
  2. Since you are passing in the uri, with the EXTRA_OUTPUT key, MediaStore will save the file to that location. So in the onActivityResult callback, you just check if the message is ok and the request code corresponds, after that you can read from the file with the URI you've passed in with the EXTRA_OUTPUT key

Upvotes: 1

Mutaz Alfezani
Mutaz Alfezani

Reputation: 71

in onActivtyResult check, if URI not null pass it to your new activity

    Intent intent = Intent(this, your activity)
    intent.putExtra("uri", uri)
    startActivity(intent)
    

then in (your new activity) use these lines to get Uri

   Bitmap photo= BitmapFactory.decodeFile(intent.getStringExtra("uri"))
   imageView.setImageBitmap(photo)

NOTE: pass uri as string

Upvotes: 0

Related Questions