Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getExternalCacheDir() returns null after clearing data

I have a simple app that access and writes data to external storage. Everything works fine until I go to Settings -> Apps -> App Info and clear data via "Clear data" button, then every call to getExternalCacheDir() starts returning null.

I have been developing on Nexus 7 running Android 4.2.2.

My manifest looks like:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.package"
    android:versionCode="5"
    android:versionName="1.3"
    xmlns:tools="http://schemas.android.com/tools">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
...

Code snippet that does not work:

    Log.d(TAG, "getExternalStorageState() = " + Environment.getExternalStorageState());
    Log.d(TAG, "getExternalCacheDir() = " + c.getExternalCacheDir());
    Log.d(TAG, "getExternalFilesDir(null) = " + c.getExternalFilesDir(null));
    Log.d(TAG, "getExternalFilesDir(Environment.DIRECTORY_MOVIES) = " + c.getExternalFilesDir(Environment.DIRECTORY_MOVIES));

LogCat after the app is installed and executed:

05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalStorageState() = mounted
05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalCacheDir() =     /storage/emulated/0/Android/data/com.example.package/cache
05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalFilesDir(null) = /storage/emulated/0/Android/data/com.example.package/files
05-15 11:26:45.948: DEBUG/HelperUtils(5541): getExternalFilesDir(Environment.DIRECTORY_MOVIES) = /storage/emulated/0/Android/data/com.example.package/files/Movies

LogCat after clearing data in App Info settings:

05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalStorageState() = mounted
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external cache directory
05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalCacheDir() = null
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external files directory
05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalFilesDir(null) = null
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external files directory
05-15 11:27:57.848: DEBUG/HelperUtils(5859): getExternalFilesDir(Environment.DIRECTORY_MOVIES) = null
05-15 11:27:57.848: WARN/ContextImpl(5859): Unable to create external cache directory

After clearing data and executing the app getExternalCacheDir() method returns null even though Environment.getExternalStorageState() returns "mounted". Does anyone know what could be possibly wrong?

EDIT

With the help from Gjordis I have found out that Clear data button removes whole application temporary directory:

storage/sdcard0/Android/data/com.example.app/cache in Android/data

and I was not able to create it again via getExternalCacheDir() or manually (though I am able to create other directories under storage/sdcard0/Android/data/).

(Android/data/com.example.app is created again after the device is rebooted, but that is not the solution I am looking for)

like image 764
Ondřej Z Avatar asked May 15 '13 10:05

Ondřej Z


2 Answers

I have encountered and solved this exact problem.

Clicking the Clear Data button causes Android to stop your app from running and delete the entire application-specific folder "mnt/sdcard/Android/data/your.package.name" .

However, I had a separate process that was started from Runtime.getRuntime().exec() that was still running and it was writing to this folder. This caused the folder to be stuck in a locked state and caused the same symptom you described when my app called getExternalCacheDir() . Running adb shell and then ls from within the /mnt/sdcard/Android/data folder showed that the folder was locked by a process. Running ps showed that my other process was still running.

The solution was to properly kill the other process that was still writing to my application's application-specific folder before calling getExternalCacheDir() .

like image 148
Robert Avatar answered Nov 12 '22 16:11

Robert


getExternalCacheDir() returns cache dir like the name says. If there is no cache, there is no directory for it either. This directory is used for temporary files you removed with remove data command. In cases where phone is low on space, it can remove these folders itself too. Atleast some maintenance applications do so.

getExternalFilesDir() returns the directory for space to save data.

like image 6
Gjordis Avatar answered Nov 12 '22 15:11

Gjordis