Android File(packed in Application)
We sometimes use file in Android Application.
In that case, we save file into application. Where is it?
Location
We have 2 choices to save file by default.
- res/raw
- assets
Several people explain between 2. So I don’t need additional description about them.
Basically, we use assets, but in case of using sound file or image resource to manage by R.java,
you need to use res/raw.
** res/raw is not existed by default. make it!**
** To manage file in those directory, we can get InputStream only, can’t get File class**
Samples
readFromAssets reads file from assets directory
readFromRaw reads file from res/raw
I saved test1.txt in res/raw, test2.txt in assets
public class ApplicationFileActivity extends Activity
{
private static String TAG = "Application File";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
readFromAssets();
readFromRaw();
new FileOperations(this).readFromAssets();
}
private void readFromAssets()
{
AssetManager as = getResources().getAssets();
BufferedReader br = null;
try
{
InputStream in = as.open("test2.txt");
br = new BufferedReader(new InputStreamReader(in));
String line;
Log.i(TAG, "Assets");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
private void readFromRaw()
{
InputStream in = getResources().openRawResource(R.raw.test); // test.txt
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
try
{
Log.i(TAG, "Raw");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
}
Sample2 Use Context
public class FileOperations
{
private static String TAG = "File Operations";
Context context;
public FileOperations ( Context context )
{
this.context = context;
}
public void readFromAssets()
{
AssetManager as = context.getResources().getAssets();
BufferedReader br = null;
try
{
InputStream in = as.open("test2.txt");
br = new BufferedReader(new InputStreamReader(in));
String line;
Log.i(TAG, "Assets");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
public void readFromRaw()
{
InputStream in = context.getResources().openRawResource(R.raw.test); // test.txt
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
try
{
Log.i(TAG, "Raw");
while ( (line = br.readLine()) != null )
{
Log.i(TAG, line);
}
}
catch ( IOException oops )
{
oops.printStackTrace();
}
finally
{
if ( br != null )
{
try
{
br.close();
}
catch(IOException oops )
{
oops.printStackTrace();
}
}
}
}
}
To call this from Activity,
new FileOperations(this).readFromAssets();
Problem
Basically, these directory is managed by android.content.Context.
We can’t get file path and can’t get File class.
We can get only InputStream.
