user557240
user557240

Reputation: 273

Android where to store editable files

My app reads a bunch of text files. At the moment I have saved them all in the assets folder but I would like to edit these files at runtime.

Should copy all those files into internal storage on the first run or can I create a folder in eclipse and save the files.

If so what would be the filepath to read them?

Thanks.

Upvotes: 1

Views: 277

Answers (4)

Puckl
Puckl

Reputation: 741

If you added the files to your Assets folder in Eclipse, they will be loaded with your app and you can access them at runtime with

InputStream in = context.getAssets().open(filename);

And then you can use the InputStream as usual. You can get the context in your Activity with getApplicationContext().

If you want to write files:

FileOutputStream fOut = context.openFileOutput(fileName, Context.MODE_PRIVATE);

Upvotes: 1

Sameer
Sameer

Reputation: 4389

You should copy those files to internal storage as you mentioned. There is no need to use external storage if the files are private to the application. When you say storing files in a folder in Eclipse, I think you are really talking about creating a folder that will be shipped with your APK. Such a file will not be writable.

Upvotes: 0

sokie
sokie

Reputation: 1996

Android gives you everything you need: if you want to save on external storage you can simply do

Environment.getExternalStorageDirectory().getPath()

what i would recomend is saving it in cache dir and edit them when needed,as it seems to fit best to your needs.

getCacheDir().getPath()

but do note that:

Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.

PS: you can check if you can write on external directory this way:

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
//  to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}

as shown data storage And you can simply do that if possible write on external dir,otherwise fall back to internal cache.

Also you can write anywhere on the filesystem but make the file private to your application

FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);

Upvotes: 1

Sebastian Breit
Sebastian Breit

Reputation: 6159

You can save the files in the external storage :

Environment.getExternalStorageDirectory()

Upvotes: 0

Related Questions