title: Where should I write a temporary file?
body: <p>In android devices external memory may or may not be present.</p>
  
  <p>I am writing an app which in one case stream mp3 from URL &amp; stores it in a file in memory for temporary purpose.</p>
  
  <p>But I want to be sure that, that file will get created.</p>
  
  <p>First I thought of using  <a href="http://developer.android.com/reference/android/content/Context.html#getCacheDir%28%29">getCacheDir</a> , but it is mentioned:</p>
  
  <blockquote>
    <p>These files will be ones that get deleted first when the device runs
    low on storage. There is no guarantee when these files will be
    deleted. Note: you should not rely on the system deleting these files
    for you; you should always have a reasonable maximum, such as 1 MB,
    for the amount of space you consume with cache files, and prune those
    files when exceeding that space.</p>
  </blockquote>
  
  <p>So, then I thought of using  <a href="http://developer.android.com/reference/android/content/Context.html#getExternalCacheDir%28%29">getExternalCacheDir</a> , but then I'm not sure that the device will contain external memory or not.</p>
  
  <p>There is one more optional issue:
  Before saving the file in memory, I want to check if the directory has enough space. How can this be done?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I also thought of creating a file in <code>Environment.getDownloadCacheDirectory()</code>, as probably it will be available on every device. </p>
  
  <pre><code>File f3 = new File(Environment.getDownloadCacheDirectory(), "Temp.txt");
      if (!f3.exists()) {
          f3.createNewFile();
      } 
  </code></pre>
  
  <p>But got below error trace in createNewFile():</p>
  
  <pre><code>java.io.IOException: Permission denied
      at java.io.File.createNewFileImpl(Native Method)
      at java.io.File.createNewFile(File.java:1257)
      at com.example.activitylifecycle.MyStreamingApp.startStream(MyStreamingApp.java:42)
      at com.example.activitylifecycle.MyStreamingApp.onCreate(MyStreamingApp.java:23)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
      at android.app.ActivityThread.access$1500(ActivityThread.java:135)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
      at android.os.Handler.dispatchMessage(Handler.java:99)
      at android.os.Looper.loop(Looper.java:150)
      at android.app.ActivityThread.main(ActivityThread.java:4385)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:507)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
      at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Manifest File:</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.activitylifecycle"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="15" /&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.activitylifecycle.ActivityLifeCycle"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".MyStreamingApp"
              android:label="@string/app_name" &gt;
              &lt;intent-filter android:priority="100" &gt;
                  &lt;action android:name="com.example.activitylifecycle.MYSTREAMINGAPP" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Please suggest.
  Thank You</p>