如何在Android Studio中启动相机?

问题描述 投票:0回答:1

单击摄像机图标时,我将看到一个对话框,其中显示如下两个选项->

1:拍摄新图像

2:从存储器中选择图像

当我使用第二个选项时,将从手机存储器中选择的图像被保存。但是当使用第一个选项时,相机单击的图像不会被保存。我没有得到错误可能是什么。谁能帮我这个忙吗?

  `public class ChangePhotoDialog extends DialogFragment {

private static final String TAG = "ChangePhotoDialog";

public interface OnPhotoReceivedListener{
    public void getBitmapImage(Bitmap bitmap);
    public void getImagePath(String imagePath);
}

OnPhotoReceivedListener mOnPhotoReceived;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_changephoto, container, false);

    //initalize the textview for starting the camera
    TextView takePhoto = (TextView) view.findViewById(R.id.dialogTakePhoto);
    takePhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: starting camera.");
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, Init.CAMERA_REQUEST_CODE);
        }
    });

    //Initialize the textview for choosing an image from memory
    TextView selectPhoto = (TextView) view.findViewById(R.id.dialogChoosePhoto);
    selectPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: accessing phones memory.");
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, Init.PICKFILE_REQUEST_CODE);
        }
    });

    // Cancel button for closing the dialog
    TextView cancelDialog = (TextView) view.findViewById(R.id.dialogCancel);
    cancelDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: closing dialog.");
            getDialog().dismiss();
        }
    });

    return view;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try{
        mOnPhotoReceived = (OnPhotoReceivedListener) getTargetFragment();
    }catch (ClassCastException e){
        Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage() );
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    /*
    REsults when taking a new image with camera
     */
    if(requestCode == Init.CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK){
        Log.d(TAG, "onActivityResult: done taking a picture.");

        //get the new image bitmap
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        Log.d(TAG, "onActivityResult: receieved bitmap: " + bitmap);

        //send the bitmap and fragment to the interface
        mOnPhotoReceived.getBitmapImage(bitmap);
        getDialog().dismiss();
    }

    /*
    Results when selecting new image from phone memory
     */
    if(requestCode == Init.PICKFILE_REQUEST_CODE && resultCode == Activity.RESULT_OK){
        Uri selectedImageUri = data.getData();
        File file = new File(selectedImageUri.toString());
        Log.d(TAG, "onActivityResult: images: " + file.getPath());


        //send the bitmap and fragment to the interface
        mOnPhotoReceived.getImagePath(file.getPath());
        getDialog().dismiss();

    }
}

}`

android-camera
1个回答
0
投票

您是否可以发送logcat(error),也许您没有权限(android.permission.CAMERA)。

© www.soinside.com 2019 - 2024. All rights reserved.