title: Android write files to USB via OTG cable
body: <p>I've been searching for many topics about android file writing, yet most of them wanted to write files to android internal storage. Others who wanted to write files on external SD card didn't success at all. My case is quite similar but I think that writing files to external USB is a totally different case.</p>
  <p>I am using Samsung galaxy Note II running stock TouchWiz 4.4.2 [not rooted]. My phone supports micro-USB-OTG and I can mount my USB as rwxrwx--x without rooting. The complete path of my USB is /storage/UsbDriveA.</p>
  <p>I've tried to use Environment.getExternalStorageDirectory() to get the path or  use the path (mentioned above) directly but neither of them succeed. The first one returns internal storage path and the second one returns an error with &quot;permission denied&quot;. I have already put the</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  </code></pre>
  <p>in Android Manifest so I wondered why my code didn't work.</p>
  <p>Moreover, I can write anything to my USB using Root Browser (use it without root) and Simple Browser thus I believe that there's a way to do that.</p>
  <p>Here's my code:</p>
  <pre><code>File file = new File(path.getAbsolutePath(), &quot;test.txt&quot;);
  // File file = new File(&quot;/storage/extSdCard&quot;,&quot;test.txt&quot;);
  err = false;
    try {
      FileOutputStream f = new FileOutputStream(file);
      PrintWriter pw = new PrintWriter(f);
      pw.print(get);
      pw.flush();
      pw.close();
      f.close();
    }catch (Exception e) {
      e.printStackTrace();
      Toast.makeText(MainActivity.this, &quot;writing error&quot;,Toast.LENGTH_LONG).show();
                  err = true;
     }
     Log.i(&quot;File Path:&quot;, file.getPath());
  </code></pre>