1

Some background.
Use case: supply office type file from my application to one of the microsoft apps in a way that microsoft app could save changes on the same file (not save a copy).

The problem is microsoft saving method. As observed it first creates two other files before modifying the original file. This denies me from supplying file from my app private directory as those are not writable to other apps. So my conclusion is that I need public directory.

Tried solutions (from public storage):
SAF - not working. If I try to give file which was created from OPEN_DOCUMENT_TREE uri microsoft app only lets me save edits as a copy.
FileProvider - same as above.
Custom ContentProvider implementation - this somewhat works but for this I need permissions for all user storage (and on SDK 30 MANAGE_EXTERNAL_STORAGE which is currently restricted) and feels hacky (eg. microsoft app asks for DATA column which is deprecated). Here's the code:

class OfficeContentProviderHelper : ContentProvider() {
    // Omitted not used overrided methods

    override fun query(uri: Uri, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
        // Testing showed that this works
        return if (projection?.singleOrNull() == MediaStore.MediaColumns.DATA) {
            val cursor = MatrixCursor(projection, 1)
            cursor.addRow(setOf(uri.path))
            cursor
        } else {
            null
        }
    }

    override fun getType(uri: Uri): String? {
        val path = uri.path
        val extension = path!!.substringAfterLast('.')
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
    }

    companion object {
        fun getUriForFile(filePath: String): Uri =
            Uri.Builder()
                .scheme("content")
                .authority("${BuildConfig.APPLICATION_ID}.officeprovider")
                .appendPath(filePath)
                .build()
    }
}

Question - is there a way to do something like this without asking for storage permissions as they feel kinda overkill for this usecase, if not is this implementation valid? As in how do you create ContentProvider for public storage files without any database, because I couldn't find any recent non-github information which would support my usecase.

EDIT following posts show the same problem:

  1. When using Android file provider, files don't have correct permissions despite FLAG_GRANT_WRITE_URI_PERMISSION being flagged in intent
  2. Cannot open word file in edit mode in android using intent
  3. How do I open a Word Document as an Editable file using an Intent on Android?
MJegorovas
  • 750
  • 1
  • 6
  • 18
  • 1
    We're currently struggling with the same issues, trying to edit Office files using FileProvider, and want the files to be saved back to our sandbox. Have you made any progress? Our app does not qualify for the [MANAGE_EXTERNAL_STORAGE permission](https://support.google.com/googleplay/android-developer/answer/10467955?visit_id=637673067198487986-32469158&rd=1#zippy=%2Cpermitted-uses-of-the-all-files-access-permission), as this is not a core functionality of our app, so this is not an option for us. – TomasL Sep 15 '21 at 13:05
  • Unfortunately no. I decided that added complexity for interacting with outside apps was too much to maintain and for security reasons opted out of this feature. Generally I am mad how apps don't implement `ACTION_EDIT` intent properly (text, photo edit apps etc.) by sending the result back to origin application. – MJegorovas Sep 21 '21 at 08:54
  • 1
    It turns out that the Google Document app shows up as an option when running `ACTION_EDIT` on .docx files. Editing works fine and saves back to the original file. FYI, on .pdf-files, the Microsoft OneDrive PDF-viewer works fine for `ACTION_EDIT` – TomasL Sep 22 '21 at 11:38
  • @TomasL Could you explain a bit more how did you manage to do that ? i am facing the exact same problem and i'n having trouble to use FileProvider to open a file in the download/document folder to edit the file. It works perfectly with the old way (file://...) – tlvi38 May 24 '23 at 10:42

0 Answers0