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: