Android一起从图库或相机显示选项中选择图像

时间:2016-12-12 09:13:36

标签: android android-camera android-gallery

在我的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);

我将首先看到允许从图库中选择图像的选项,然后在向下滑动之后,我会看到允许相机的选项。如何在一个弹出窗口中显示它?

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作....

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

希望这对你有用!!