title: Android permission denied when saving file from a non-UI thread
body: <p>I having trouble with the download of an image from a new thread and saving it into a file.</p>
  
  <p>on some devices it works flawelsly </p>
  
  <p>on others, such as the HTC INCERDIBLE, I am getting an exception</p>
  
  <p>I managed to figure out it is in this line:</p>
  
  <pre><code>InputStream is=new URL(url).openStream();
  </code></pre>
  
  <p>this is the complete stack dump</p>
  
  <pre><code>import java.io.InputStream;
  
  Bitmap bitmap=null;
  InputStream is=new URL(url).openStream();
  OutputStream os = new FileOutputStream(f);
  Utils.CopyStream(is, os);
  os.close();
  </code></pre>
  
  <p><br /></p>
  
  <pre><code>D/dalvikvm( 1930): GC_EXPLICIT freed 26K, 47% free 3642K/6855K, external 0K/0K, paused 90ms
  W/System.err( 2669): java.io.FileNotFoundException: /mnt/sdcard/com.example.testing1/remote-image-cache/74110ca30e217cfdcc78a98f814de093c217bc07.hdr (Permission denied)
  W/System.err( 2669):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
  W/System.err( 2669):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
  W/System.err( 2669):    at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:94)
  W/System.err( 2669):    at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:66)
  W/System.err( 2669):    at java.io.FileWriter.&lt;init&gt;(FileWriter.java:42)
  W/System.err( 2669):    at org.appcelerator.titanium.util.TiResponseCache.put(TiResponseCache.java:359)
  W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.maybeCache(HttpURLConnectionImpl.java:331)
  W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1043)
  W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
  W/System.err( 2669):    at java.net.URL.openStream(URL.java:645)
  W/System.err( 2669):    at ti.testing.ImageLoader.getBitmap(ImageLoader.java:163)
  W/System.err( 2669):    at ti.testing.ImageLoader.access$100(ImageLoader.java:29)
  W/System.err( 2669):    at ti.testing.ImageLoader$PhotosLoader.run(ImageLoader.java:256)
  </code></pre>
  
  <p>My manifest.xml file includes many permissions:</p>
  
  <pre><code>&lt;/application&gt;
  
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission android:name="android.permission.VIBRATE"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>
  
  <p>this is how I am getting the file path</p>
  
  <pre><code>   if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
          cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
      else
          cacheDir=context.getCacheDir();
  </code></pre>
  
  <p>I don't understand it... Ideas ?</p>
  
  <h3>EDIT</h3>
  
  <p>I tried changing:</p>
  
  <pre><code>cacheDir=new File("/mnt/","emmc"); 
  </code></pre>
  
  <p>but now I get:</p>
  
  <pre><code>W/System.err( 404): java.io.FileNotFoundException: /mnt/emmc/cp_1216407347.jpeg 
  (No such file or directory)
  </code></pre>
  
  <hr>
  
  <p>I tried changing cacheDir=new File("/mnt/","emmc"); but now I get W/System.err( 404): java.io.FileNotFoundException: /mnt/emmc/cp_1216407347.jpeg (No such file or directory)</p>