简体   繁体   中英

Getting the path of chosen image from gallery intent

Here's my function for the camera intent. I get the path by storing it first in a variable, and then using it to set the file destination.

private void openCameraIntent() {
    Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = new File(getExternalCacheDir(), "image_1.jpeg");
    fileUri = Uri.fromFile(file);
    pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(pictureIntent, REQUEST_CAPTURE_IMAGE);
}

However, I cannot do the same to gallery since the image that will be chosen is already there.

private void openGalleryIntent() {
    Intent gallery = new Intent(Intent.ACTION_PICK,
            MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, REQUEST_CAPTURE_IMAGE);
}

Is there any way that I can get the path within this particular function?

You can instead use the recommended Activity Result API to pick up the image from gallery/storage which returns the URI of the picked image.

private val pickImage = 
registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
    yourMethod(uri)  
}
pickImage.launch("image/*")

Refer to google docs for more information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM