Friday, January 4, 2019

How to Select Image from Gallery with Intents in android

How to Select Image from Gallery with Intents in android Choose an image from the gallery that’ll be displayed by the application (and e... thumbnail 1 summary

How to Select Image from Gallery with Intents in android

Choose an image from the gallery that’ll be displayed by the application (and even uploaded to your servers) after the selection is made. In this article we’ll see how to invoke a single interface from which the user is able to select images across all his apps (like Gallery, Photos, ES File Explorer, etc.) and folders (Google Drive, Recent, Downloads, etc.) using Intents.

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser
 (intent, "Select Picture"), PICK_IMAGE_REQUEST);


PICK_IMAGE_REQUEST is the request code defined as an instance variable.
 
private int PICK_IMAGE_REQUEST = 1;

Now once the selection has been made, we’ll show up the image in the Activity/Fragment user interface, using an ImageView. For this, we’ll have to override onActivityResult():
 
 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri uri = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                img_view.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


How to Select Image from Gallery with Intents in android

No comments

Post a Comment