Getting "Failed to find configured root that contains /data/data/" exception Android

Viewed 123

I am trying to share a screenshot of my activity.

I am creating a bitmap like this

fun createImageToShare(): Bitmap {
        val b =
            Bitmap.createBitmap(
                recyclerViewGroups.getWidth(),
                recyclerViewGroups.getHeight(),
                Bitmap.Config.ARGB_8888
            )
        val c = Canvas(b)
        recyclerViewGroups.draw(c)
        return b
    }

Then I am trying to save the bitmap to a file like this

fun saveBitmapToFile(bitmap: Bitmap): File? {
        try {
            val dir = File(this.cacheDir,"myfolder").apply { mkdirs() }
            val fileName = "myfile.jpg"
            val file = File(
                dir,
                fileName
            )
            println("file path ${file.path}")
            val fOut = FileOutputStream(file)


            bitmap.compress(
                Bitmap.CompressFormat.JPEG,
                85,
                fOut
            ) 
            fOut.close()
            return file
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

Then I am trying to share this file using SHARE_INTENT like this

R.id.menu_share -> {
                try {
                    val bmp = createImageToShare()
                    val str = saveBitmapToFile(bmp)
                    val uri = FileProvider.getUriForFile(this,"com.mypackage.myrepo.fileprovider",str!!)
                    println("URI PATH $uri")
                    val shareIntent: Intent = Intent().apply {
                        action = Intent.ACTION_SEND
                        putExtra(Intent.EXTRA_STREAM, uri)
                        type = "image/jpeg"
                    }
                    startActivity(Intent.createChooser(shareIntent, "Share to"))
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }

I have Permissions and Provider like this on my Manifest file

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Inside the Application tag

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.mypackage.myrepo.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

This is the content of my file_paths

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path
        name="myrepo"
        path="myfolder/" />
    <external-files-path
        name="myrepo"
        path="myfolder/"/>
</paths>

So when I try to share this image, the file path printed is

file path /data/user/0/com.mypackage.myrepo/cache/myfolder/myfile.jpg

and the exception I get is

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.mypackage.myrepo/cache/myfolder/myfile.jpg
    at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
    at com.mypackage.myrepo.group.GroupActivity.onOptionsItemSelected(GroupActivity.kt:109)
    at android.app.Activity.onMenuItemSelected(Activity.java:3543)

I just want to share an image of my activity. The file location can be internal or external but nothing seems to be working. Any help would be hugely appreciated. Thanks

0 Answers
Related