问题
I'm trying to use FileProvider to share a SQL database file via email.
Error:
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.columbiawestengineering.columbiawest/databases/testresults.db
My code:
<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());
Also, Logcat for goob shows the correct db location:
....../com.columbiawestengineering.columbiawest D/LOG PRINT SHARE DB: File goob..? getDatabasePath.. here it is: /data/data/com.columbiawestengineering.columbiawest/databases/testresults.db
Any help?
From developer.android, it appears that the xml files-path... represents the files/ subdirectory. But that is not where the file is stored. I'm at a loss.
回答1:
Also, Logcat for goob shows the correct db location:
Yes, but that's not where <files-path>
points to. Given that database path, the equivalent getFilesDir()
would be:
/data/data/com.columbiawestengineering.columbiawest/files
Hence, your database is not inside the getFilesDir()
directory, which is what <files-path>
uses, which is why FileProvider
is not happy. FileProvider
does not support sharing content from the database directory.
回答2:
Solved (Thanks @CommonsWare). FileProvider cannot access the SQL database file because it is in the databases directory. I just copied the file from the databases dir to the files dir (so that FileProvider can get to it), added permissions, attached it to email, and then deleted the db from the files dir when email sent with the start and onActivityForResult() methods.
My Java looks like this now:
//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());
and here is what I did to copy the file:
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;
}
来源:https://stackoverflow.com/questions/36464615/android-fileprovider-failed-to-find-configured-root