We are creating an android application. I want to perform file operations in succession, but when I start the picker and write to saf (strage access framework) in succession, only the first file is written and the subsequent files are not written.
What I would like to achieve
I want to start and write the picker continuously in saf and make all files work properly.
Source code for this.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(0..2).forEach {
val intent = Intent().apply {
type = "*/*"
action = Intent.ACTION_CREATE_DOCUMENT
putExtra(Intent.EXTRA_TITLE, "test$it.txt")
putExtra(
DocumentsContract.EXTRA_INITIAL_URI,
Uri.parse("content://com.android.externalstorage.documents/document/primary%3ADownload")
)
}
testFileCreate.launch(intent)
}
}
private val testFileCreate = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
try {
contentResolver.openOutputStream(result.data?.data!!) .use { outputStream ->
outputStream?.write("testdata".toByteArray())
}
} catch (e: Exception) {
e.printStackTrace())
}
}
}
What we tried.
When I started and closed each one individually, it worked fine, but when I started the next one before closing the previous saf, it did not work except for the first one.
I was able to create it using ActivityResultContracts.OpenDocumentTree(), but since android12, I cannot use OpenDocumentTree because the download folder is not available. Also, we are not using MANAGE_EXTERNAL_STORAGE because it may not pass the screening process and because it works more stably using a picker (filer).
Supplemental information (FW/tool versions, etc.)
android 12