2016-04-07 11 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()); 

はまた、goobためLogcatは正しいデシベルの場所を示しています

....../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 

したがって、データベースはFileProviderが幸せではない理由は何か<files-path>用途でgetFilesDir()ディレクトリ、内ではありません。 FileProviderは、データベースディレクトリからのコンテンツの共有をサポートしていません。

+0

おかげCommonsWare、それは私が恐れていたものです...他のはありますデータベースファイルを取得する方法?私はそれを文字列に印刷して文字列をメールで送ることができますが、実際のデータベースファイルを電子メールで送信しようとしています。 –

+1

@DanLehto: 'Context'の[' getDatabasePath() '](http://developer.android.com/reference/android/content/Context.html#getDatabasePath(java.lang.String))はあなたに' File'オブジェクトがデータベースを指しています。 'FileProvider'の目的のために、ファイルのコピーを' getFilesDir() 'または' getCacheDir() 'に作ることは大歓迎です。その時点でデータベースが使用されていないことを確認してください。または、数日後に[私の 'StreamProvider'](https://github.com/commonsguy/cwac-provider)にアップデートして、あなたのデータベースを直接提供できるように拡張する必要があります。 – CommonsWare

2

解決済み(Thanks @ CommonsWare) FileProviderはデータベースディレクトリにあるため、SQLデータベースファイルにアクセスできません。私はちょうどデータベースdirからファイルdirにファイルをコピーして(FileProviderがそれにアクセスできるように)、アクセス権を追加して、電子メールに添付してから、startとonActivityForResult()メソッド。

//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()); 

、ここでは、私は、ファイルをコピーするためにやったことです:私のJavaは次のようになります

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; 
} 
関連する問題