2016-04-07 129 views
0

我想通过电子邮件使用FileProvider共享SQL数据库文件。Android:FileProvider“无法找到配置的根”

错误:

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中显示goob正确的DB位置:

....../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 ...代表文件/子目录。但这不是文件存储的地方。我很茫然。

回答

1

Also, Logcat for goob shows the correct db location:

是的,但这不是<files-path>指向的地方。鉴于数据库路径,等效getFilesDir()将是:

/data/data/com.columbiawestengineering.columbiawest/files 

因此,你的数据库是不是getFilesDir()目录,这是什么<files-path>用途,这就是为什么FileProvider不开心里。 FileProvider不支持从数据库目录共享内容。

+0

感谢CommonsWare,这就是我害怕......难道还有其他的获取数据库文件的方法?我可以将它打印到一个字符串并通过电子邮件发送字符串,但是我想通过电子邮件发送实际的数据库文件 –

+1

@DanLehto:[''getDatabasePath()''Context'](http://developer.android.com/reference/android/content/Context.html#getDatabasePath(java.lang.String))会返回给你一个'文件对象指向你的数据库。为了达到FileProvider的目的,欢迎您将该文件复制到'getFilesDir()'或'getCacheDir()'。只要确保你的数据库当时没有被使用。或者,我将在接下来的几天内推出[我的'StreamProvider'](https://github.com/commonsguy/cwac-provider)更新,您应该能够将其扩展为支持直接为您的数据库提供服务。 – CommonsWare

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; 
} 
相关问题