簡體   English   中英

圖庫按鈕打開相機和圖庫

[英]Gallery button opens camera and gallery

在我的應用程序中,用戶可以從圖庫或相機將圖像上傳到我的服務器,然后將這些圖像轉換為 PDF

我用於訪問相機和圖庫的按鈕位於對話框中,

現在我的問題是,當您 select 畫廊時,它會在畫廊頂部打開相機(所以當您退出相機時,它會將您帶到畫廊)

我正在使用 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);

        }
    }

因為問題是您錯過了case 0Break語句。 這就是為什么它同時執行這兩種情況。

代碼應該是這樣的:

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

     default:
     break;


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM