Advertisements

CS Select Image Button Clicked

    //Request Code for starting activity for result method
    private final int PICK_IMAGE = 001;
    //Saving image uri so that we can use it to upload the file
    private Uri imageUri;

    //This function will be called when "Select Image" button will be clicked
    public void oneClicked(View view) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    }


    //This function will be called when user selects an image and returns to app
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //We need to check if the request code is same and user actually picked some image
        if(requestCode ==PICK_IMAGE && resultCode == RESULT_OK){
            //Getting the Image URI
            imageUri = data.getData();
            try{
                //Setting the image to ImageView
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
                mImageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }