user1212520
user1212520

Reputation: 501

Android save path and creating files

I am trying to save some data for a android game, but I cant create the file.

File file = new File(filename);
file.createNewFile();

or

File file = new File(filename);
file.mkdir();

What path should I set? The code above doesn't work. I think I have the read/write correct, but I can't make the file initially (and yes I have googled it)

It is a permanent file for the app.

Upvotes: 0

Views: 4229

Answers (1)

Jeffrey Blattman
Jeffrey Blattman

Reputation: 22647

If it's a large file, you should create it on external storage (SD card). you can get the path to the ext. Storage by calling Environment.getExternalStorageDirectory(). so,

File file = new File(Environment.getExternalStorageDirectory(), "myfile.txt");

There is of course no permission on the external storage file system (because it's a FAT file system). If you need the file to be private to your app, you can use Context.openFileOutput(),

OutputStream os = context.OpenFileOutput("myfile.txt", MODE_PRIVATE);

This creates the file in a the app-specific data directory on internal storage. The file is private to the UID of your app, so it cannot be read by other applications.

Upvotes: 1

Related Questions