从图库中选择图像或从相机中取出

时间:2016-03-30 10:25:47

标签: android camera gallery

我必须制作一个按钮,用于从图库中选择图像或从相机中取出。

 private void showFileChooser() {
    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);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch(requestCode) {
        case 0:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                imageView.setImageURI(selectedImage);
            }

            break;
        case 1:
            if(resultCode == RESULT_OK){
                Uri selectedImage = imageReturnedIntent.getData();
                imageView.setImageURI(selectedImage);
            }
            break;
    }
}

结果正常。如果我从图库中选择,图像查看器将查看它,如果我选择从相机拍照,它也可以工作。 问题是,在我的showFileChooser()方法中,我的所有意图都在同一时间运行,所以当我从图库中选择时,相机仍在运行。我选择相机,画廊也开放。我想我应该在switch case模式下实现我的代码,但我不明白该怎么做。请帮助解决我的初学者问题。

3 个答案:

答案 0 :(得分:5)

添加alertDialog然后在onActivityResult而不是0和1的情况下使用REQUEST_CAMERASELECT_FILE。您将在代码中声明:

private static final int REQUEST_CAMERA = 1;
private static final int SELECT_FILE = 2;

final CharSequence[] items = {"Take Photo", "Choose from Library", "Cancel"};
            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mActivity);
            builder.setTitle("Add Photo!");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {

                    if (items[item].equals("Take Photo")) {
                        PROFILE_PIC_COUNT = 1;
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent, REQUEST_CAMERA);
                    } else if (items[item].equals("Choose from Library")) {
                        PROFILE_PIC_COUNT = 1;
                        Intent intent = new Intent(
                                Intent.ACTION_PICK,
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(intent,SELECT_FILE);
                    } else if (items[item].equals("Cancel")) {
                        PROFILE_PIC_COUNT = 0;
                        dialog.dismiss();
                    }
                }
            }); 
            builder.show();

答案 1 :(得分:1)

您应首先决定要执行的操作,如下所示

  public class PicModeSelectDialogFragment extends DialogFragment {

        private String[] picMode = {"Camera", "Gallery"};

        private IPicModeSelectListener iPicModeSelectListener;

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setItems(picMode, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (iPicModeSelectListener != null)
                        iPicModeSelectListener.onPicModeSelected(Constants.PicModes.values()[which]);
                }
            });
            return builder.create();
        }

        public void setiPicModeSelectListener(IPicModeSelectListener iPicModeSelectListener) {
            this.iPicModeSelectListener = iPicModeSelectListener;
        }

        public interface IPicModeSelectListener {
            void onPicModeSelected(Constants.PicModes modes);
        }
    }

之后你就可以开启意图打开相机或画廊,如下所示:

if(openCamera){
  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
}else{
//open gallery
 Intent intent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
} 

答案 2 :(得分:0)

首先创建一个带有两个按钮的对话框,要求用户选择GalleryCamera.点击按钮设置Intent以选择如下图像:

public void dialog()
    {
        final Dialog dialog = new Dialog(getActivity(), R.style.cust_dialog);
        dialog.setTitle("Upload From");
        dialog.setContentView(R.layout.dialog_pop_up_gallery_camera);

        dialog.setTitle("Select an Option...");
        TextView txt_gallry=(TextView)dialog.findViewById(R.id.textView_gallery);
        TextView txt_camera=(TextView)dialog.findViewById(R.id.textView_camera);

        txt_gallry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                             Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            i.setType("image/*");
                            startActivityForResult(i, PHOTO_PICKER_ID);
            }
        });
        txt_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();



                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                File fil = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fil));
                startActivityForResult(cameraIntent, REQUEST_CODE_CAPTURE_IMAGE);
            }
        });
        dialog.show();
    }

以下是对话框的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center|center_vertical"
              android:background="@color/lightpurple">

    <TextView
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="Gallery"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:padding="10dp"

        android:drawableLeft="@drawable/galery_small"
        android:drawablePadding="15dp"
        android:id="@+id/textView_gallery"/>
    <TextView
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:text="Camera"
        android:drawablePadding="15dp"
        android:drawableLeft="@drawable/camera"
        android:padding="10dp"
        android:id="@+id/textView_camera"/>
</LinearLayout>