0

I'm trying to get an backup file from downloads folder by an intent. The intent picker work's fine

 val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
                intent.type = "application/zip"
                intent.flags = FLAG_GRANT_READ_URI_PERMISSION
                registerForActivityResult.launch(intent)

in the file result i'm trying like this:

try {
 val path: String = data.dataString.toString()
                
 val uri = FileProvider.getUriForFile(this, applicationContext.packageName.toString() + ".provider", File(path))

  val file = File(uri.path.toString())
  if (file.exists()) {
      restoreDatabase()
  }
  else{
   //error
  }

} catch (e: Exception) {
            Log.e("pickFile", e.toString())
        }

int the exception i'm getting the error (the error ocurrs in line: val uri = FileProvider.getUriForFile..... ):

"java.lang.IllegalArgumentException: Failed to find configured root that contains /content:/com.android.externalstorage.documents/document/primary%3ADownload%2Fdatabase.zip"

i have an provider configured:

  <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="myPackage.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

providers xml

<paths>
    <external-files-path
        name="external_files"
        path="." />
    <external-path
        name="external"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>
  • `intent.flags = FLAG_GRANT_READ_URI_PERMISSION` Remove that line. You cannot grat anything. – blackapps Nov 24 '21 at 20:45
  • You do not need to use FileProvider to get an uri. You already got an uri in onActivityResult. Use that uri. – blackapps Nov 24 '21 at 20:46
  • And.. do not try to get a File instance from an uri. Not needed as you can use the uri directly. Better tell who should do the backup. Just copy a file? Or restore a database with it? – blackapps Nov 24 '21 at 20:48
  • `restoreDatabase()` No parameter? Use the uri: `restoreDatabase(data.data)`. – blackapps Nov 24 '21 at 20:52
  • FWIW, [this sample project](https://gitlab.com/commonsguy/cw-room/-/tree/v0.9/ImportExport) shows database backup and restore (or import and export) using `OpenDocument` and `CreateDocument` contracts. – CommonsWare Nov 24 '21 at 20:54
  • My file is zipped, i need to create an file to unzip. If i try to create a file from data.data, a get the error: java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/primary%3ADownload%2Fdatabase.zip: open failed: ENOENT (No such file or directory) – Gabriel Ricci Nov 25 '21 at 00:40

0 Answers0