views:

788

answers:

2

In Android's settings, in the "Manage Applications" activity when clicking on an app, the data is broken down into Application, Data, and cache. There is also a button to clear the cache. My app caches audio files and I would like the user to be able to clear the cache using this button. How do I store them so they get lumped in with the cache and the user can clear them? I've tried storing files using both of the following techniques:

newFile = File.createTempFile("mcb", ".mp3", context.getCacheDir());


newFile = new File(context.getCacheDir(), "mcb.mp3");
newFile.createNewFile();

In both cases, these files are listed as Data and not Cache.

+3  A: 

I think you have to make use of android:allowClearUserData and android:manageSpaceActivity under your <application> tag to see that option in 'Manage Applications'. See http://developer.android.com/guide/topics/manifest/application-element.html for more information.

Alternatively, have an option for clearing cache in your activity.

Al
It appears allowClearUserData will not work, but manageSpaceActivity is a good idea. http://www.mail-archive.com/[email protected]/msg16507.html However, I would still like to be able to use the clear cache button.
Jay Askren
+7  A: 

I can't reproduce your problem, perhaps something else is wrong.

Using the following application, I was able to create a file in the cache directory and then clear it using the Manage Applications function under Settings. I didn't have to change anything in the manifest and from what I can tell, you really shouldn't mess with the android:allowClearUserData option.

public class CreateCacheFile extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.CreateFileButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(
                        CreateCacheFile.this.getCacheDir(), "temp.txt");

                try {
                    file.createNewFile();
                    FileWriter fw = new FileWriter(file);
                    BufferedWriter bw = new BufferedWriter(fw);
                    bw.write("Hello World");
                    bw.newLine();
                    bw.close();

                } catch (IOException e) {
                    Toast.makeText(
                            CreateCacheFile.this, 
                            "Error!", 
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

After running the application (and clicking the button to create the file):

$ adb -d shell
# ls /data/data/com.example.CreateCacheFile/cache
temp.txt
# cat /data/data/com.example.CreateCacheFile/cache/temp.txt
Hello World
# 

Manage Applications reports that 4KB of space is in use for Cache. After clicking the button to clear it:

# ls /data/data/com.example.CreateCacheFile/cache
# cat /data/data/com.example.CreateCacheFile/cache/temp.txt
temp.txt: No such file or directory
# 

At this point Manage Applications reports that 0KB of space is in use for Cache.

Tim Kryger
Fantastic, worked for me. +1
greenie
You are right. Seems to work. I must be doing something else wrong. Thanks.
Jay Askren