代码之家  ›  专栏  ›  技术社区  ›  Shashwat

java.lang.illegalargumentException:未能找到包含/data/data的已配置根目录/

  •  1
  • Shashwat  · 技术社区  · 8 年前

    我正在尝试使用action_image_capture intent捕获图像,然后将其保存到/data/data/**/app_profile/profile_picture.jpg 我用以下代码来表示意图:

    val cw = ContextWrapper(this@UserActivity.applicationContext)
    val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)
    
    output = File(dir, "/profile_picture.jpg")
    intent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
    
    **val newPhotoUri = FileProvider.getUriForFile(applicationContext, "$packageName.fileprovider", output)**
    
    intent.putExtra(MediaStore.EXTRA_OUTPUT, newPhotoUri)
    startActivityForResult(intent, REQUEST_CAMERA)
    
    Log.e(TAG, "output: ${output.absolutePath}") ///data/user/0/**/app_profile/profile_picture.jpg
    

    但我发现下面的车祸发生了 FileProvider.getUriForFile()

    java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/**/app_profile/profile_picture.jpg
        at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
        at **.UserActivity.takePhoto(UserActivity.kt:147)
    

    我在清单中设置了以下内容:

    <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="**.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepaths" />
    </provider>
    

    我的filepaths.xml:

    <paths>
        <files-path name="app_profile" path="." />
    </paths>
    

    我已经试过了

    <files-path name="app_profile" path="app_profile/" />
    

    还有很多其他的组合。 有人能引导我通过吗?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Shashwat    8 年前

    val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)
    

    val dir:File = cw.filesDir
    

        2
  •  0
  •   rockgecko    7 年前

    FileProvider

    <root-path name="root_files" path="." />

    WRITE_EXTERNAL_STORAGE context.getDir()

    context.getFilesDir()