Unable to make new folder in Apps cache folder
<p>I have written a caching system. It saves data to the cache folder of the App using platform / Android methods to find the folder. I have an issue that I cannot create a new folder in the Apps cache folder.</p>

<p>This error is reporting in Crashlytics (via non-fatal error reporting) for <em>some</em> devices and <em>not</em> for the majority. </p>

<p>The method that contains the issue is my <code>getCacheFolder()</code> method.</p>

<p>An example folder path that does not work on these devices: <code>/storage/emulated/0/Android/data/app.package.name/cache/stream-cache</code>.</p>

<p>Reported versions affected: <code>4.4.2</code>; <code>5.0.1</code>; <code>6.0</code>, <code>6.0.1</code>.</p>

<p>Reported devices affected: <code>LENOVO YOGA Tablet 2-1050L</code>, <code>MOTOROLA MotoE2(4G-LTE)</code>, <code>Samsung SM-N910F</code> (accounts for 98% of faults reported).</p>

<p>My suspicions are that there might be an issue relating to removable sdcards.</p>

<pre><code>@NonNull
public File getCacheFolder() {
    // If this is the first run of this library function, we should get our cache folder
    if (mCacheFolder == null || !mCacheFolder.exists()) {
        File dir = null;

        // Prioritise the external sdcard to store cached files
        dir = mContext.get().getExternalCacheDir();
        if(dir == null) {
            // There was no external sdcard - instead use internal storage
            dir = mContext.get().getCacheDir();
        }

        // Still couldn't get a cache location, go to fallback plan and throw exception
        if(dir == null) {
            throw new IllegalStateException("Could not create a location to cache. " +
                    "This leaves the caching library in a bad state.");
        }

        // Point to our specific caching folder for this system
        mCacheFolder = new File(dir, "stream-cache");
    }

    // If it doesn't exist, we will need to make it
    if (!mCacheFolder.exists()) {
        // If we cannot make the directory, go to fallback plan and throw an exception
        if (!mCacheFolder.mkdirs()) {
            // HERE IS WHERE THE FAULT IS REPORTED
            throw new IllegalStateException("Could not create a location to cache. " +
                    "This leaves the caching library in a bad state.: "+mCacheFolder);
        }
    }

    return mCacheFolder;
}
</code></pre>

<h2>Update 1</h2>

<p>I updated the code to write out various information about the state:</p>

<pre><code>@NonNull
public File getCacheFolder() {
    if (mCacheFolder == null || !mCacheFolder.exists()) {
        File dir = mContext.get().getExternalCacheDir();
        if(dir == null || !Utils.isExternalStorageWritable()) {
            dir = mContext.get().getCacheDir();
        }

        mCacheFolder = new File(dir, "stream-cache");
    }

    if (!mCacheFolder.exists()) {
        if (!mCacheFolder.mkdirs()) {
            // Problems writing to cache - test writing to file path.
            File filePath = mContext.get().getExternalFilesDir(null);
            File cachePath = new File(filePath, "stream-cache");

            throw new IllegalStateException("Could not create a location to cache. " +
                    "This leaves the caching library in a bad state.: "+mCacheFolder+ ". " +
                    "filePath="+filePath+". " +
                    "cachePath="+cachePath+". " +
                    "cachePath-exists="+cachePath.exists()+". " +
                    "cachePath-mkdirs="+cachePath.mkdirs());
        }
    }

    return mCacheFolder;
}
</code></pre>

<p>The important results from this:</p>

<p>filePath=null - <code>Context.getExternalFilesDir(null);</code> returns a null. Breaking the rest of the tests.</p>