Marc
Marc

Reputation: 41

What is my directory when writing files in Android?

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();

When trying to delete one of those files, this is what I use, but it's returning false.

String tag = v.getTag().toString();
File file = new File(System.getProperty("user.dir")+"/"+tag);
String s = new Boolean (file.exists()).toString();
Toast.makeText(getApplicationContext(), s, 1500).show();
file.delete();

How can I overcome this problem?

Upvotes: 1

Views: 6150

Answers (4)

Manju
Manju

Reputation: 742

String RootDir = Environment.getExternalStorageDirectory()
                + File.separator + "Video";
        File RootFile = new File(RootDir);
        RootFile.mkdir();

            FileOutputStream f = new FileOutputStream(new File(RootFile, "Sample.mp4"));

i used this code to save the video files to non-default location. Hope this will be useful to you.By default it is storing in sd card

For each application the Android system creates a "data/data/package of the application" directory. Files are saved in the "files" folder under this directory

to change the default directory the above code will be used

the default working directory can be displayed using fileobject.getAbsolutePath()

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

Use getFileStreamPath(FILENAME) to find your file. From the docs:

Returns the absolute path on the filesystem where a file created with openFileOutput(String, int) is stored.

Upvotes: 5

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

Your current working directory.

To help diagnose the problem, use file.getAbsolutePath() to see the full path.

It could also be a permissions problem, if you're trying to delete from another application. If so, you may need to change to MODE_WORLD_WRITEABLE (insecure), or restructure your code so the create and delete are called by the same app.

EDIT: That was mostly incorrect. I didn't realize that openFileOutput didn't use the current working directory.

Upvotes: 3

Shaunak
Shaunak

Reputation: 18008

Use same contents as 'FILENAME' variable in your first snippet in the second snippet while trying to delete.

Upvotes: 1

Related Questions