Android:FileProvider“找不到配置的根”

时间:2016-04-07 00:28:29

标签: android android-studio android-fileprovider

我正在尝试使用FileProvider通过电子邮件共享SQL数据库文件。

错误:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.columbiawestengineering.columbiawest/databases/testresults.db

我的代码:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="test_results" path="databases/"/>
</paths>

的Manifest.xml:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.columbiawestengineering.columbiawest"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

Java.code:

    File goob = this.getDatabasePath("testresults.db");
    Log.d("LOG PRINT SHARE DB", "File goob..? getDatabasePath.. here it is: " + goob.toString());

    Uri contentUri = FileProvider.getUriForFile(this, "com.columbiawestengineering.columbiawest", goob);
    Log.d("LOG PRINT SHARE DB", "contentUri got: here is contentUri: " + contentUri.toString());

此外,Logcat for goob显示正确的数据库位置:

....../com.columbiawestengineering.columbiawest D/LOG PRINT SHARE DB: File goob..? getDatabasePath.. here it is: /data/data/com.columbiawestengineering.columbiawest/databases/testresults.db

任何帮助?

从developer.android看来,xml files-path ...表示文件/子目录。但这不是文件存储的位置。我不知所措。

2 个答案:

答案 0 :(得分:2)

解决了(感谢@CommonsWare)。 FileProvider无法访问SQL数据库文件,因为它位于数据库目录中。我只是将文件从数据库目录复制到文件目录(以便FileProvider可以访问它),添加权限,将其附加到电子邮件,然后在使用start和onActivityForResult()发送电子邮件时从文件目录中删除数据库方法。

我的Java现在看起来像这样:

    //this copies the .db file from dabases dir where FileProvider cannot access it and moves it to files dir
    File booger = copyFileToFilesDir("testresults.db");
    Log.d("LOG PRINT SHARE DB", "we found a booger, Here it is: " + booger.toString());

    Uri contentUri = FileProvider.getUriForFile(this, "com.columbiawestengineering.columbiawest", booger);
    Log.d("LOG PRINT SHARE DB", "contentUri got: here is contentUri: " + contentUri.toString());

以下是我复制文件的方法:

private File copyFileToFilesDir(String fileName) {
    File file = null;
    String newPath = getFileStreamPath("").toString();
    Log.d("LOG PRINT SHARE DB", "newPath found, Here is string: " + newPath);
    String oldPath = getDatabasePath("testresults.db").toString();
    Log.d("LOG PRINT SHARE DB", "oldPath found, Her is string: " + oldPath);
    try {
        File f = new File(newPath);
        f.mkdirs();
        FileInputStream fin = new FileInputStream(oldPath);
        FileOutputStream fos = new FileOutputStream(newPath + "/" + fileName);
        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ((len1 = fin.read(buffer)) != -1) {
            fos.write(buffer, 0, len1);
        }
        fin.close();
        fos.close();
        file = new File(newPath + "/" + fileName);
        if (file.exists())
            return file;
    } catch (Exception e) {

    }
    return null;
}

答案 1 :(得分:1)

  

此外,Logcat for goob显示正确的数据库位置:

是的,但那不是 // Expand the CardView private boolean expand(View cardView) { // Make the shortened ("intro") text invisible and make the full text visible mIntroTextView.setVisibility(View.GONE); mExpandableIndicator.setVisibility(View.GONE); mCollapseIndicator.setVisibility(View.VISIBLE); mInfoTextView.setVisibility(View.VISIBLE); return true; } // Collapse the CardView private boolean collapse(View cardView) { // Make the shortened ("intro") text visible and make the full text invisible mInfoTextView.setVisibility(View.GONE); mCollapseIndicator.setVisibility(View.GONE); mExpandableIndicator.setVisibility(View.VISIBLE); mIntroTextView.setVisibility(View.VISIBLE); return false; } 指向的地方。鉴于该数据库路径,等效的<files-path>将是:

getFilesDir()

因此,您的数据库不在/data/data/com.columbiawestengineering.columbiawest/files 目录中,这是getFilesDir()使用的目录,这就是<files-path>不满意的原因。 FileProvider不支持从数据库目录共享内容。