Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获得;找不到包含/data/data/”的配置根目录;例外Android_Android_Kotlin_Android Bitmap_Android File_Android Fileprovider - Fatal编程技术网

获得;找不到包含/data/data/”的配置根目录;例外Android

获得;找不到包含/data/data/”的配置根目录;例外Android,android,kotlin,android-bitmap,android-file,android-fileprovider,Android,Kotlin,Android Bitmap,Android File,Android Fileprovider,我正在尝试分享我的活动截图 我正在创建一个像这样的位图 fun createImageToShare(): Bitmap { val b = Bitmap.createBitmap( recyclerViewGroups.getWidth(), recyclerViewGroups.getHeight(), Bitmap.Config.ARGB_8888

我正在尝试分享我的活动截图

我正在创建一个像这样的位图

fun createImageToShare(): Bitmap {
        val b =
            Bitmap.createBitmap(
                recyclerViewGroups.getWidth(),
                recyclerViewGroups.getHeight(),
                Bitmap.Config.ARGB_8888
            )
        val c = Canvas(b)
        recyclerViewGroups.draw(c)
        return b
    }
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
    }
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()
                }
            }
然后我尝试将位图保存到这样的文件中

fun createImageToShare(): Bitmap {
        val b =
            Bitmap.createBitmap(
                recyclerViewGroups.getWidth(),
                recyclerViewGroups.getHeight(),
                Bitmap.Config.ARGB_8888
            )
        val c = Canvas(b)
        recyclerViewGroups.draw(c)
        return b
    }
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
    }
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()
                }
            }
然后我试着像这样使用share_意图来共享这个文件

fun createImageToShare(): Bitmap {
        val b =
            Bitmap.createBitmap(
                recyclerViewGroups.getWidth(),
                recyclerViewGroups.getHeight(),
                Bitmap.Config.ARGB_8888
            )
        val c = Canvas(b)
        recyclerViewGroups.draw(c)
        return b
    }
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
    }
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()
                }
            }
我在清单文件上有这样的
权限
提供者

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这是我的
文件路径的内容

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path
        name="myrepo"
        path="myfolder/" />
    <external-files-path
        name="myrepo"
        path="myfolder/"/>
</paths>
我得到的例外是

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)

我只想分享我活动的一个图像。文件位置可以是内部的,也可以是外部的,但似乎什么都不起作用。任何帮助都将不胜感激。谢谢

嗯,乍一看,这些都是正确的。在先前构建之后,是否修改了
文件路径
以添加
?你是否尝试过清理/重建缓存,甚至使缓存失效/重新启动?@MikeM。试过所有的方法还是一样的错误