简体   繁体   中英

Gallery button opens camera and gallery

In My app a user can upload images to my server from the gallery or camera,these images are then converted to PDF

My buttons for accessing the camera and gallery are in a dialog,

Now my problem is that when you select the gallery it opens the camera on top of the gallery(so when you exit out of the camera it takes you to the gallery)

I am using PhotoUtils Library for Image Processing

https://github.com/kosalgeek/PhotoUtil

Here is My code for the Dialog

AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
        pictureDialog.setTitle("Select Action");
        String[] pictureDialogItems = {
                "Select photo from gallery",
                "Capture photo from camera"};
        pictureDialog.setItems(pictureDialogItems,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                if (ContextCompat.checkSelfPermission(SecondActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                                    getImageFromGallery();
                                } else {
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                        if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                                            Toast.makeText(getApplicationContext(), "Permission Needed.", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PICK_IMAGE);
                                    }
                                }
                            case 1:
                                if (ContextCompat.checkSelfPermission(SecondActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                                    try {
                                        getImageFromCamera();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                        if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                                            Toast.makeText(getApplicationContext(), "Permission Needed.", Toast.LENGTH_LONG).show();
                                        }
                                    }
                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                        requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_CAMERA);
                                    }
                                }
                                break;

                        }
                    }
                });
        pictureDialog.show();
    }

Here is the camera Intent code

private void getImageFromCamera() throws IOException {

        Button upload=findViewById(R.id.upload);
        upload.setVisibility(View.VISIBLE);
        cameraPhoto = new CameraPhoto(getApplicationContext());
        Intent in = cameraPhoto.takePhotoIntent();
        startActivityForResult(in, REQUEST_CAMERA);
        Bungee.split(SecondActivity.this);

Here is the code for the Gallery Intent

 private void getImageFromGallery() {
        Button upload=findViewById(R.id.upload);
        upload.setVisibility(View.VISIBLE);
        Intent intent=galleryPhoto.openGalleryIntent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(Intent.createChooser(intent, "Select Pictures"), PICK_IMAGE);
            Bungee.split(SecondActivity.this);

        }
    }

Because the issue is you've missed the Break statement for case 0 . That's why it is executing both cases.

Code should be like:

switch (which) 
{
     case 0:
        // Your logic
     break;
     case 1:
        // Your logic
     break;

     default:
     break;


}

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