In my app it is supposed that users can choose a path to save their texts through SAF. I use ActivityResultContracts.CreateDocument
to do this, which will provide the Uri of the created file so that I can operate on that file, such as writing contents to its outputStream. However, although the file is created successfully, its content is empty. it seems that the callback, in which I write contents with the given Uri, is never called. The codes are not running, and the logs are not printed. I looked up the official documents and forums, trying to find examples of ActivityResultContracts.CreateDocument and found nothing but that the constructor I used to create the contract, which receives no argument, is deprecated and replaced by a new one that requires mime type of the created file like:
public final CreateDocument(@NonNull String mimeType)
However, in my Anroid Studio I can only find and use the deprecated constructor like following:
@RequiresApi(19)
open class CreateDocument : ActivityResultContract<String, Uri?>() {
...
}
I wonder if it's the reason behind the not-working onResult callback, or it's because my codes meet some problems, and how I can solve this problem. My codes are below:
// WriteScreen.kt(Composable)
// Launched after clicking a button.
val saveFile = rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument()) { uri ->
Log.d(TAG, "WriteScreen: Enter callback onResult of saveFile launcher")
uri?.let { viewModel.saveFileAs(it) }
}
...
// WriteViewModel.kt
fun saveFileAs(uri: Uri) {
Log.d(TAG, "saveFileAs: enter method saveFileAs")
viewModelScope.launch(Dispatchers.IO) {
Application.context.contentResolver.openOutputStream(uri)?.writer()?.run {
write(_uiState.value.content)
flush()
close()
Log.d(TAG, "saveFileAs: saved content: ${_uiState.value.content}")
}
}
}
My targetSdkVersion is 32, and compose version is 1.2.0-beta02.