0

I followed this youtube tutorial to add a camera button to my app but the app seems to crash when it gets to

val fileProvider = FileProvider.getUriForFile(this, "com.example.fileprovider", photoFile)

I'm not very sure if it's definately this line but I ran the debugger and it seems to crash here.

Full snippet below:

       cameraButton.setOnClickListener {
            val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            photoFile = getPhotoFile(FILE_NAME)

            try{
                val fileProvider = FileProvider.getUriForFile(this, "com.example.fileprovider", photoFile)
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider)
                if(takePictureIntent.resolveActivity(this.packageManager) != null){
                    startActivityForResult(takePictureIntent, 123)
                } else {
                    Toast.makeText(this, "Unable to open camera.", Toast.LENGTH_SHORT).show()
                }
            } catch (someException:Error){
                Toast.makeText(this, someException.toString(), Toast.LENGTH_SHORT).show()
            }
        }

    private fun getPhotoFile(fileName: String): File {
        val storageDirectory = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        return File.createTempFile(fileName, ".jpg", storageDirectory)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(requestCode == 123 && resultCode == Activity.RESULT_OK){
            //val takenImage = data?.extras?.get("data") as Bitmap
            val takenImage = BitmapFactory.decodeFile(photoFile.absolutePath)
            iv_cam.setImageBitmap(takenImage)
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

AndroidManifest:

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

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <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="com.example.tubinginfieldinspectiontool.MainActivity"
            android:windowSoftInputMode="stateVisible|adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:authorities="com.example.fileprovider"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/path">
            </meta-data>
        </provider>
    </application>
</manifest>

Path.xml:

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

Stack Trace:

2020-06-14 13:24:30.661 26865-26865/com.example.tubinginfieldinspectiontool E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.tubinginfieldinspectiontool, PID: 26865
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.tubinginfieldinspectiontool/files/Pictures/photo6352906109530684106.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.example.tubinginfieldinspectiontool.MainActivity$onCreate$3.onClick(MainActivity.kt:68)
        at android.view.View.performClick(View.java:7357)
        at android.widget.TextView.performClick(TextView.java:14210)
        at android.view.View.performClickInternal(View.java:7323)
        at android.view.View.access$3200(View.java:849)
        at android.view.View$PerformClick.run(View.java:27884)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7266)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
  • Possibly just a simple typo: `"com.example.fileproviderr"`. You have an extra `r` at the end of that one in the manifest, so the authority won't match. We would need to see the complete [stack trace from the crash](https://stackoverflow.com/a/23353174) to be certain of the current issue, though. – Mike M. Jun 14 '20 at 03:01
  • Hi @MikeM. thanks for showing me that! I fixed the typo and the error still occurs. I will add the stack trace to the original post. – Tiuing Gum Jun 14 '20 at 03:20
  • For `getExternalFilesDir()`, you want to use an `` element, instead of ``. – Mike M. Jun 14 '20 at 03:28
  • @MikeM. It worked! The image shows up in the app now but when I check the actual save location, there's no file in there? Is there piece of code I'm missing to permanently save the image in the directory? Also, let me know if I should make a separate thread for that question. – Tiuing Gum Jun 14 '20 at 03:56
  • Nah, you're good. As long as we don't get too far away from the original topic. How exactly are you checking for that file? I mean, are you using some file browser directly on the device? Or one on your development machine? Or maybe in Android Studio? Are you using adb, possibly? – Mike M. Jun 14 '20 at 04:06
  • @MikeM. I'm looking at the file through explorer on my computer (tablet is plugged in). I am using Android Studio to make the app. – Tiuing Gum Jun 14 '20 at 04:11
  • @MikeM. Files are now showing up and everything is working good. Looking at the directory through my computer doesn't seem to update unless I unplug and plug back in the tablet. Weird but it works! Thank you for your help! – Tiuing Gum Jun 14 '20 at 04:19
  • Yep, exactly. That's actually expected behavior, because what your computer is seeing is not actually the file system, but kind of a "snapshot" that only gets updated periodically, or on reboots, etc. You can also explicitly trigger an immediate scan on your new file from your code, if you would need that file immediately visible, for whatever reason. If not, then you don't really need to worry about that part. Anyhoo, I'm gonna mark this as a duplicate for the original issue, so this shows as resolved to everyone. Glad you got it working. Cheers! – Mike M. Jun 14 '20 at 04:29

0 Answers0