0

In my Android Studio code, I am trying with the captureImage method to open the camera (below the code). When this method is called, I get a FATAL EXCEPTION (below the details). Reading the error, it seems that it was looking for a certain /storage/emulated/0/Android/data/com.example.ves.gennaio3/files/Pictures/OCR_201903162231401331084905.jpg that I am not calling anywhere. How can this be possible? How can I fix this issue?

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>
Stefano
  • 209
  • 2
  • 10
  • 34
  • 1
    Your problem lies in your `FileProvider` configuration. Your `` element in the manifest for `FileProvider` should have a `` child element, which in turn points to an XML resource configuring `FileProvider`. If you edit your question and post that XML resource, we can point out the specific problem. – CommonsWare Mar 16 '19 at 21:52
  • 1
    Possible duplicate of [FileProvider throws exception on GetUriForFile](https://stackoverflow.com/questions/21931169/fileprovider-throws-exception-on-geturiforfile) – denis_lor Mar 16 '19 at 21:54
  • 1
    Possible duplicate also of [FileProvider - IllegalArgumentException: Failed to find configured root](https://stackoverflow.com/q/42516126/3564632) – denis_lor Mar 16 '19 at 21:55
  • @CommonsWare thank you. I have added the info that you asked. Tell me if they are not enough. – Stefano Mar 24 '19 at 16:24
  • @denis_lor thank you but I have already read those links and they are not effective for my situation. – Stefano Mar 24 '19 at 16:25

1 Answers1

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

This says that you want to serve up files that are located in a my_images/ subdirectory under the location returned by getExternalFilesDir(null).

The path that you are using is:

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

That is for a Pictures/ subdirectory under the location returned by getExternalFilesDir(null). Since my_images does not match Pictures, FileProvider does not know how to serve that file.

Either:

  • Change your file path to use my_images instead of Pictures, or

  • Change your <external-files-path> to have path="Pictures" instead of path="my_images"

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491