Create document with SAF and rename it

80 Views Asked by At

I am getting result Uri from ACTION_CREATE_DOCUMENT intent.

Since not all formats has mime type I want to set file manually with correct extension.

I'm using DocumentsContract.renameDocument() but this function only work after I write data to the file, what I want is to rename and then write data, but the problem that after this function is called I don't have write permission anymore.

How can I grant permission after DocumentsContract.renameDocument() is called? without extra permissions.

1

There are 1 best solutions below

0
Yurowitz On

What about using extras to rename the file during the activity result ? Here's how I would do it:

// Set the file name for the document
val fileName = "new_file_name.txt"

// Create the intent
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.putExtra(Intent.EXTRA_TITLE, fileName)

// Launch the intent
startActivityForResult(intent, CREATE_DOCUMENT_REQUEST_CODE)