如何在 Java Android Studio 错误中使用 Android 相机拍照

How to take a picture with Android Camera in Java Android Studio Error

我正在编写一个应用程序,我想要一个按钮,它可以使用 android 设备上的后置摄像头拍照。但是,我不想预览正在拍摄的图像(或表面视图)或打开 Android 相机应用程序。我一直在尝试在 android 开发者网站 https://developer.android.com/training/camera/photobasics#java

上实施本教程

我不断收到以下错误:

知道出了什么问题吗?

2019-12-06 08:30:04.602 18879-18879/com.example.MyProject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.MyProject, PID: 18879
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:7140)
        at android.view.View.performClickInternal(View.java:7117)
        at android.view.View.access00(View.java:801)
        at android.view.View$PerformClick.run(View.java:27351)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7140) 
        at android.view.View.performClickInternal(View.java:7117) 
        at android.view.View.access00(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27351) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.MyProject/files/Pictures/JPEG_20191206_083004_2041148828713793256.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.example.MyProject.MainActivity.dispatchTakePictureIntent(MainActivity.java:259)
        at com.example.MyProject.MainActivity.startImageCapture(MainActivity.java:170)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:7140) 
        at android.view.View.performClickInternal(View.java:7117) 
        at android.view.View.access00(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27351) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

这是我目前拥有的代码。 MainActivity.java

public class MainActivity extends AppCompatActivity {

    //for taking photos
    static final int REQUEST_IMAGE_CAPTURE = 1;
    String currentPhotoPath;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    @Override
    public void onResume(){
        super.onResume();


    }


//button to start image capturing process
    public void startImageCapture(View view){

        dispatchTakePictureIntent();
        galleryAddPic();


    }


//method for taking a photo
    public void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //Ensure that there is a camera activity to handle the intent
        if(takePictureIntent.resolveActivity(getPackageManager()) != null){
            //create the file where the photo should go
            File photoFile = null;
            try{
                photoFile = createImageFile();
            }catch(IOException e){
                Log.i("ERROR","Error in trying to create file for image");
            }
            //continue only if the file was successfully created
            if (photoFile != null){
                Uri photoURI = FileProvider.getUriForFile(this,"com.example.MyProject",photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
            }



        }
    }


    private File createImageFile() throws IOException{
        //Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName, //prefix
                ".jpg", //suffix
                storageDir //directory
        );

        //Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }



    private void galleryAddPic(){
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(currentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }



}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.MyProject">


    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>




    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <provider
            android:authorities="com.example.MyProject"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>

        </provider>

    </application>

</manifest>

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="storage/self/primary/DCIM/Camera" />
</paths>

更新1

//added unique file path in android manifest and MainActivity

//AndroidMainifest.xml
android:authorities="com.example.MyProject.provider"

//MainActivity.java
Uri photoURI =FileProvider.getUriForFile(this,"com.example.MyProject.provider",photoFile);

错误

2019-12-06 08:30:04.602 18879-18879/com.example.MyProject E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.MyProject, PID: 18879
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:7140)
        at android.view.View.performClickInternal(View.java:7117)
        at android.view.View.access00(View.java:801)
        at android.view.View$PerformClick.run(View.java:27351)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7140) 
        at android.view.View.performClickInternal(View.java:7117) 
        at android.view.View.access00(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27351) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.MyProject/files/Pictures/JPEG_20191206_083004_2041148828713793256.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.example.MyProject.MainActivity.dispatchTakePictureIntent(MainActivity.java:259)
        at com.example.MyProject.MainActivity.startImageCapture(MainActivity.java:170)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:7140) 
        at android.view.View.performClickInternal(View.java:7117) 
        at android.view.View.access00(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27351) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

更新2

//updated external file path in file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_images" path="/" />
</paths>

但是还是报错:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.MyProject, PID: 19805
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:7140)
        at android.view.View.performClickInternal(View.java:7117)
        at android.view.View.access00(View.java:801)
        at android.view.View$PerformClick.run(View.java:27351)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7140) 
        at android.view.View.performClickInternal(View.java:7117) 
        at android.view.View.access00(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27351) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.google.android.GoogleCamera/com.android.camera.activity.CaptureActivity clip={text/uri-list U:content://com.example.MyProject.provider/my_images/Pictures/JPEG_20191206_090412_1644275951049514201.jpg} (has extras) } from ProcessRecord{e6130f1 19805:com.example.MyProject/u0a284} (pid=19805, uid=10284) with revoked permission android.permission.CAMERA
        at android.os.Parcel.createException(Parcel.java:2071)
        at android.os.Parcel.readException(Parcel.java:2039)
        at android.os.Parcel.readException(Parcel.java:1987)
        at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3851)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1712)
        at android.app.Activity.startActivityForResult(Activity.java:5192)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
        at android.app.Activity.startActivityForResult(Activity.java:5150)
        at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
        at com.example.MyProject.MainActivity.dispatchTakePictureIntent(MainActivity.java:261)
        at com.example.MyProject.MainActivity.startImageCapture(MainActivity.java:170)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:7140) 
        at android.view.View.performClickInternal(View.java:7117) 
        at android.view.View.access00(View.java:801) 
        at android.view.View$PerformClick.run(View.java:27351) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: android.os.RemoteException: Remote stack trace:
        at com.android.server.wm.ActivityStackSupervisor.checkStartAnyActivityPermission(ActivityStackSupervisor.java:1043)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:760)
        at com.android.server.wm.ActivityStarter.startActivity(ActivityStarter.java:583)
        at com.android.server.wm.ActivityStarter.startActivityMayWait(ActivityStarter.java:1288)
        at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:514)

这个:

android:authorities="com.example.MyProject"

不匹配:

Uri photoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider",photoFile);

更具体地说,"com.example.MyProject" 不匹配 "com.example.android.fileprovider"。它们必须相同且唯一。