为什么在尝试打开相机时出现此致命错误?

时间:2019-03-16 21:48:07

标签: android

在我的Android Studio代码中,我正在尝试使用captureImage方法打开相机(在代码下方)。调用此方法时,我得到一个FATAL EXCEPTION(在详细信息下方)。读取错误后,似乎正在寻找某个我未在任何地方打电话的/storage/emulated/0/Android/data/com.example.ves.gennaio3/files/Pictures/OCR_201903162231401331084905.jpg。这怎么可能?我该如何解决这个问题?

Java

public void captureImage(View view) {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (cameraIntent.resolveActivity(getPackageManager()) != null) {
        pictureFile = Helper.getPictureFile(ReceiptActivity.this);
        if (pictureFile != null) {
            Uri photoURI = FileProvider.getUriForFile(ReceiptActivity.this, "com.example.ves.gennaio3.fileprovider", pictureFile);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(cameraIntent, REQUEST_PICTURE_CAPTURE);
        }
    }
}

Logcat

> 03-16 22:31:40.198 21436-21436/com.example.ves.gennaio3
> E/AndroidRuntime: FATAL EXCEPTION: main
>     Process: com.example.ves.gennaio3, PID: 21436
>     java.lang.IllegalStateException: Could not execute method for android:onClick
>         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
>         at android.view.View.performClick(View.java:5201)
>         at android.view.View$PerformClick.run(View.java:21163)
>         at android.os.Handler.handleCallback(Handler.java:746)
>         at android.os.Handler.dispatchMessage(Handler.java:95)
>         at android.os.Looper.loop(Looper.java:148)
>         at android.app.ActivityThread.main(ActivityThread.java:5443)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
>      Caused by: java.lang.reflect.InvocationTargetException
>         at java.lang.reflect.Method.invoke(Native Method)
>         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
>         at android.view.View.performClick(View.java:5201) 
>         at android.view.View$PerformClick.run(View.java:21163) 
>         at android.os.Handler.handleCallback(Handler.java:746) 
>         at android.os.Handler.dispatchMessage(Handler.java:95) 
>         at android.os.Looper.loop(Looper.java:148) 
>         at android.app.ActivityThread.main(ActivityThread.java:5443) 
>         at java.lang.reflect.Method.invoke(Native Method) 
>         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
>      Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains
> /storage/emulated/0/Android/data/com.example.ves.gennaio3/files/Pictures/OCR_201903162231401331084905.jpg
>         at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:738)
>         at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
>         at com.example.ves.gennaio3.ReceiptActivity.captureImage(ReceiptActivity.java:124)
>         at java.lang.reflect.Method.invoke(Native Method) 
>         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
>         at android.view.View.performClick(View.java:5201) 
>         at android.view.View$PerformClick.run(View.java:21163) 
>         at android.os.Handler.handleCallback(Handler.java:746) 
>         at android.os.Handler.dispatchMessage(Handler.java:95) 
>         at android.os.Looper.loop(Looper.java:148) 
>         at android.app.ActivityThread.main(ActivityThread.java:5443) 
>         at java.lang.reflect.Method.invoke(Native Method) 
>         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

AndroidManifest.xml

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.ves.gennaio3.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true"
            android:readPermission="com.company.app.fileprovider.READ">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_list" />
        </provider>

file_list.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="my_images" path="my_images" />
</paths>

1 个答案:

答案 0 :(得分:0)

<external-files-path name="my_images" path="my_images" />

这表示您要提供my_images/返回的位置下的getExternalFilesDir(null)子目录中的文件。

您使用的路径是:

/storage/emulated/0/Android/data/com.example.ves.gennaio3/files/Pictures/OCR_201903162231401331084905.jpg

这是Pictures/返回的位置下的getExternalFilesDir(null)子目录。由于my_imagesPictures不匹配,因此FileProvider不知道如何提供该文件。

要么:

  • 更改文件路径以使用my_images代替Pictures,或

  • 将您的<external-files-path>更改为path="Pictures",而不是path="my_images"