title: Android USB OTG storage
body: <p>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).</p>
  <p>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) – <a href="https://developer.android.com/guide/topics/connectivity/usb" rel="nofollow noreferrer">https://developer.android.com/guide/topics/connectivity/usb</a>, so maybe it is possible to write custom communication with usb storage.</p>
  <p>Anyone try something like that? Is it possible?</p>
  <hr />
  <p>Edit</p>
  <p>Now I can add single file -- here is my solution:</p>
  <pre><code>class MainActivity : AppCompatActivity() {
  
      lateinit var resultLauncher: ActivityResultLauncher&lt;Intent&gt;
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          findViewById&lt;View&gt;(R.id.button).setOnClickListener { createFile() }
  
          resultLauncher = registerForActivityResult(
              ActivityResultContracts.StartActivityForResult()
          ) { result -&gt;
              if (result.resultCode == Activity.RESULT_OK) {
                  writeInFile(result.data!!.data!!, &quot;Hello world&quot;)
              }
          }
      }
  
      private fun createFile() {
          val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
          intent.addCategory(Intent.CATEGORY_OPENABLE)
          intent.type = &quot;text/plain&quot;
          intent.putExtra(Intent.EXTRA_TITLE, &quot;Neonankiti.txt&quot;)
          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()
          }
      }
  
  }
  </code></pre>
  <p>Now I want to know how to add multiple files in directory.</p>