图库按钮可打开相机和图库

时间:2019-10-02 07:12:45

标签: java android camera

在“我的应用”中,用户可以从图库或照相机将图像上传到我的服务器,然后将这些图像转换为PDF

我用于访问相机和图库的按钮在对话框中,

现在我的问题是,当您选择图库时,它会在图库顶部打开相机(因此,当您退出相机时,它将带您进入图库)

我正在使用PhotoUtils库进行图像处理

https://github.com/kosalgeek/PhotoUtil

这是我的对话框代码

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();
    }

这是相机的意图代码

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);

这是图库意图的代码

 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);

        }
    }

1 个答案:

答案 0 :(得分:1)

因为问题是您错过了情况0 Break 语句。这就是为什么它同时执行这两种情况。

代码应类似于:

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

     default:
     break;


}