Android 正在尝试让我的应用程序拍照

Android 正在尝试让我的应用程序拍照,android,Android,这应该是一个非常简单的概念。我完全按照android开发者的文档中的说明做了,但是这段代码根本就不运行。。。当我按下按钮拍照时,应用程序停止 如果你知道原因,请告诉我 package com.example.android.health3; import android.content.Intent; import android.graphics.Bitmap; import android.provider.MediaStore; import android.support.v7.app

这应该是一个非常简单的概念。我完全按照android开发者的文档中的说明做了,但是这段代码根本就不运行。。。当我按下按钮拍照时,应用程序停止

如果你知道原因,请告诉我

package com.example.android.health3;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView result;
    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        result = (ImageView) findViewById(R.id.imageView);

    }
    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            result.setImageBitmap(imageBitmap);
        }
    }

}
文档:

使用此

相反

不要忘记在清单文件中添加权限

注:

从Android 6.0 API级别23开始,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时

有关详细信息,请尝试此选项

单击按钮上的takePicture可调用此函数


Intent cameraIntent=新IntentMediaStore.ACTION\u图像\u捕获;
startActivityForResultcameraIntent,摄像头请求

请链接文档。共享您的xml布局和崩溃登录。如果您想使用image croper,请在下面为摄像头编写pased代码。让我知道。@JustinHi非常感谢您。现在可以了。但我可以知道你为什么要把观点作为一个论据吗?因为在文档中,它不需要参数。@JustinChan检查这个,这个也可以@NileShare方法如何在DispatchTakePictureContent中使用View参数以及应该使用哪个视图?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.health3.MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="dispatchTakePictureIntent"
        android:text="Button"
      />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="208dp"
        android:layout_height="231dp"
     />

</android.support.constraint.ConstraintLayout>
public void dispatchTakePictureIntent(View v) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
}
public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
 private void takePicture() {


    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");

    String dateCreated = sdf.format(new Date(System.currentTimeMillis()));

    f = new File(getExternalCacheDir(), "IMGPLk-" + dateCreated.trim() + ".jpg");

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

    //intent.putExtra("android.intent.extras.CAMERA_FACING", 1);

    startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

    //finish();
}