Get Image from the Gallery and Show in ImageView

Because OnActivityResult method is deprecated, proper way nowadays with AndroidX Activity, is Activity Result APIs, and that recommended way, see docs

"registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you’ll use to launch the other activity."

ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
    new ActivityResultCallback<Uri>() {
        @Override
        public void onActivityResult(Uri uri) {
            previewImage.setImageURI(uri);
        }
     });

And simply call

mGetContent.launch("image/*");

when needed.


I try all the solutions above but they don't work for me . I saw a code from tutorialspoint website and it works for me very well this is the code :

    final int PICK_IMAGE = 100;

    Button button = findViewById(R.id.button33);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gallery = new Intent(Intent.ACTION_PICK,
             MediaStore.Images.Media.INTERNAL_CONTENT_URI);
             startActivityForResult(gallery, PICK_IMAGE);
        }
    });


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

    Uri imageUri;
    ImageView imageView = findViewById(R.id.image_main_galary);

    if (resultCode == RESULT_OK && reqCode == 100){
        imageUri = data.getData();
        imageView.setImageURI(imageUri);
    }
    }

the most important issue : don't forget PERMISSION :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

I use this code: This code used to start gallery activity.

 Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
 photoPickerIntent.setType("image/*");
 startActivityForResult(photoPickerIntent, GALLERY_REQUEST);

And get the result in:

 @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
         if(resultCode == Activity.RESULT_OK)
         switch (requestCode){
             case GALLERY_REQUEST:
                 Uri selectedImage = data.getData();
                 try {
                     Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                     carImage.setImageBitmap(bitmap);
                 } catch (IOException e) {
                     Log.i("TAG", "Some exception " + e);
                 }
                 break;
         }
     }

And don't forget to declare permission in AndroidManifest.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

you can try this.

paste this code in your button click event.

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);

and below code is your on activity result

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


        if (resultCode == RESULT_OK) {
            try {
                final Uri imageUri = data.getData();
                final InputStream imageStream = getContentResolver().openInputStream(imageUri);
                final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                image_view.setImageBitmap(selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
            }

        }else {
            Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
        }
    }

I hope it is helpful for you.