title: Android : Create a file in /data/local/tmp from an automated test
body: <p><strong>Goal:</strong>
  Taking a screenshot during an automated test on a device. Pull the screenshot file using adb once the test is done.</p>
  
  <p><strong>Context:</strong> </p>
  
  <p>I'm currently trying to write automated tests to take snapshots of the device screen. Using <a href="http://developer.android.com/reference/android/support/test/uiautomator/UiDevice.html" rel="nofollow noreferrer">UiDevice</a> to navigate, I would like  to take a screenshot in the middle of a test. UiDevice has a method <a href="http://developer.android.com/reference/android/support/test/uiautomator/UiDevice.html#takeScreenshot(java.io.File,%20float,%20int)" rel="nofollow noreferrer">takeScreenshot</a> that I call when I would like to take a snapshot.</p>
  
  <p>After some investigation, I realised that the class responsible to write the image into file <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/com/android/uiautomator/core/UiAutomatorBridge.java#UiAutomatorBridge.takeScreenshot%28java.io.File%2Cint%29" rel="nofollow noreferrer">UiAutomatorBridge</a> catches an Exception : </p>
  
  <blockquote>
    <p>java.io.FileNotFoundException:
    /data/local/tmp/screenshots/screen2.png: open failed: EACCES
    (Permission denied)</p>
  </blockquote>
  
  <p>Using <a href="http://developer.android.com/tools/help/adb.html" rel="nofollow noreferrer">adb</a>, I created the file and set all permissions to all users.</p>
  
  <pre><code>adb shell touch /data/local/tmp/screenshots/screen1.png
  adb shell chmod 777 /data/local/tmp/screenshots
  </code></pre>
  
  <p>Once done, I can take a screenshot with : </p>
  
  <pre><code>@Test
  public void takeSnapShot() {
      String filename = "/data/local/tmp/screenshots/screen1.png";
      File file = new File(filename);
      assertEquals(true, mDevice.takeScreenshot(file));
  }
  </code></pre>
  
  <p><strong>Problem :</strong> </p>
  
  <p>I would like to be able to create a file directly while the test is executing, without the need of using adb. 
  I tried to create a file directly from Java using <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createNewFile()" rel="nofollow noreferrer">createNewFile</a>.</p>
  
  <pre><code>try {
       file.createNewFile();
  } catch (IOException e) {
       e.printStackTrace();
  }
  </code></pre>
  
  <p>But I get an IOException</p>
  
  <blockquote>
    <p>java.io.IOException: open failed: EACCES (Permission denied)</p>
  </blockquote>
  
  <p>Has anyone an idea of what is going on ? I'm quite new to Linux, so don't hesitate to suggest something even if it seems obvious to you ;)</p>
  
  <p>Should I post this on <a href="https://superuser.com/">superuser</a> instead ? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The directory <em>/data</em> has these permissions</p>
  
  <pre><code>drwxrwx--x system   system            2016-01-14 14:03 data
  </code></pre>
  
  <p>I can't list the content of <em>/data</em>, which makes me believe the user "shell" doesn't belong to the group "system". I can't list the content of <em>/data/local</em> neither.</p>
  
  <p>However, the <em>/data/local/tmp</em> is owned by "shell". </p>
  
  <pre><code>drwxrwx--x shell    shell             2016-01-14 12:20 . (tmp)
  </code></pre>
  
  <p><em>/data/local/tmp</em> gives +x permission to all users. </p>
  
  <p>Finally, the directory "screenshots" belongs to shell with permissions 777. </p>
  
  <pre><code>drwxrwxrwx shell    shell             2016-01-14 11:46 screenshots
  </code></pre>
  
  <p>To my understanding,  any user should be able to access <em>/data/local/tmp/screenshots</em></p>