Recipe 10.4 Saving Files to the Cache Directory
Files stored in the files folder are persistent; they will always be there until you explicitly delete them. Sometimes, however, you may not want this to happen because you only need to save the files temporarily and do not need them again later. For this purpose, you should save the files into the cache folder.
Solution
To save files to the cache directory, use the getCacheDir() method to return the absolute path to the application-specific cache directory on the filesystem. You can then use the File class to create a full path for the file that you want to store in the cache folder.
The following code snippet shows how to save a file into the cache folder and then read back its content:
package net.learn2develop.cache;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast; public class MainActivity extends Activity { static final int READ_BLOCK_SIZE ...
Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.