簡體   English   中英

Android一起從圖庫或相機顯示選項中選擇圖像

[英]Android chose image from gallery or camera show option together

在我的onClick方法上,我調用了兩個新的意圖,一個是用相機拍照,另一個是從圖庫中選擇圖像。 但是,每當我在開始活動之前允許這兩個意圖時,允許對話的彈出窗口總是彼此疊加。

Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);

Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);

我將首先看到允許從圖庫中選擇圖像的選項,並將其向下滑動后,再看到允許照相機的選項。 如何在一個彈出窗口中顯示?

您可以執行以下操作。

private void showPickImageDialog() {
    AlertDialog.Builder builderSingle = new AlertDialog.Builder(MainActivity.this);
    builderSingle.setTitle("Select One Option");

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            MainActivity.this,
            android.R.layout.select_dialog_singlechoice);
    arrayAdapter.add("Gallery");
    arrayAdapter.add("Camera");

    builderSingle.setNegativeButton(
            "cancel",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    builderSingle.setAdapter(
            arrayAdapter,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            Intent pickPhoto = new Intent(Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(pickPhoto, 1);
                            break;

                        case 1:
                            Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            startActivityForResult(takePicture, 0);
                            break;
                    }

                }
            });
    builderSingle.show();
}

希望這對你有用!

暫無
暫無

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

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