I want to export my app data on USB storage. As I read before in many threads it is not possible to write secondary storage (primary is always device).
I'm considering that if it's possible to connect any devices like pad or else and write custom communication with it (in host mode) – https://developer.android.com/guide/topics/connectivity/usb, so maybe it is possible to write custom communication with usb storage.
Anyone try something like that? Is it possible?
Edit
Now I can add single file -- here is my solution:
class MainActivity : AppCompatActivity() {
lateinit var resultLauncher: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<View>(R.id.button).setOnClickListener { createFile() }
resultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
writeInFile(result.data!!.data!!, "Hello world")
}
}
}
private fun createFile() {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TITLE, "Neonankiti.txt")
resultLauncher.launch(intent)
}
private fun writeInFile(uri: Uri, text: String) {
val outputStream: OutputStream?
try {
outputStream = contentResolver.openOutputStream(uri)
val bw = BufferedWriter(OutputStreamWriter(outputStream))
bw.write(text)
bw.flush()
bw.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
Now I want to know how to add multiple files in directory.