Use case: download file from my app open it with appropriate editing application and after user is done with editing upload the file back to my app.
Example: I download the docx file from my app to public folder (eg. 'Documents') and after getting uri with FileProvider I send ACTION_VIEW intent (giving write permissions). Then open it with word app and edit it. But then the problem arives - word app says I need to save the file as a new copy and doesn't let me override the original file. Strange thing is if I open that downloaded file from file browser everything works fine and I can override the file. But when I use Intent from my app word application decides to create a copy inside their private directory. Why are they doing it this way? Is there any possibility to either get the newly saved file back to my app (from startActivityForResult) or somehow make them override the original file in the public folder?
Edit: As blackapps sugested I tried to check intents with 'Intent-Interceptor'. Here are the results:
My application:
intent://com.android.externalstorage.documents/tree/primary%3ADocuments/document/primary%3ADocuments%2FDocument.docx#Intent;scheme=content;type=application/vnd.openxmlformats-officedocument.wordprocessingml.document;launchFlags=0x13000000;end
------------
ACTION: android.intent.action.VIEW
DATA: content://com.android.externalstorage.documents/tree/primary%3ADocuments/document/primary%3ADocuments%2FDocument.docx
MIME: application/vnd.openxmlformats-officedocument.wordprocessingml.document
URI: intent://com.android.externalstorage.documents/tree/primary%3ADocuments/document/primary%3ADocuments%2FDocument.docx#Intent;scheme=content;type=application/vnd.openxmlformats-officedocument.wordprocessingml.document;launchFlags=0x13000000;end
FLAGS:
FLAG_RECEIVER_FOREGROUND
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_PREVIOUS_IS_TOP
------------
MATCHING ACTIVITIES:
Word (com.microsoft.office.word - com.microsoft.office.word.WordActivity)
Default file browser (Google 'Files'):
intent://com.google.android.apps.nbu.files.provider/1/file%3A%2F%2F%2Fstorage%2Femulated%2F0%2FDocuments%2FDocument.docx#Intent;scheme=content;type=application/vnd.openxmlformats-officedocument.wordprocessingml.document;launchFlags=0x3000000;end
------------
ACTION: android.intent.action.VIEW
DATA: content://com.google.android.apps.nbu.files.provider/1/file%3A%2F%2F%2Fstorage%2Femulated%2F0%2FDocuments%2FDocument.docx
MIME: application/vnd.openxmlformats-officedocument.wordprocessingml.document
URI: intent://com.google.android.apps.nbu.files.provider/1/file%3A%2F%2F%2Fstorage%2Femulated%2F0%2FDocuments%2FDocument.docx#Intent;scheme=content;type=application/vnd.openxmlformats-officedocument.wordprocessingml.document;launchFlags=0x3000000;end
FLAGS:
FLAG_ACTIVITY_FORWARD_RESULT
FLAG_ACTIVITY_PREVIOUS_IS_TOP
------------
MATCHING ACTIVITIES:
Word (com.microsoft.office.word - com.microsoft.office.word.WordActivity)
Here's how I obtain directory in which I save files (I use ActivityResultContracts):
directoryResultLauncher = registerForActivityResult(OpenDocumentTree()) { uri ->
if (uri == null) return@registerForActivityResult
requireContext().contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
authentication.publicDirectory = uri.toString()
}
Download method:
val directoryUriString = authentication.publicDirectory
val directory = DocumentFile.fromTreeUri(context, directoryUriString.toUri())
val file = directory!!.createFile(mimeType, title)
val fileResult = getFileContent(context.contentResolver.openOutputStream(file!!.uri)!!)
if (!fileResult.isError()) {
val intent = Intent(Intent.ACTION_VIEW, file.uri).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
startActivity(intent)
}