Marian Pavel
Marian Pavel

Reputation: 2876

Android creating file on internal storage

I am trying to create a .txt file in android, I have tested a lot of options how to achieve this, I think the file is created but I can't find it, it should be writed somehow public in a public directory that I can access it.

Example:

cacheFile = new java.io.File(getFilesDir(), "cache.txt");
        if(cacheFile.exists() && !cacheFile.isDirectory()) {
            try {
                writer = new FileWriter(cacheFile);
                BufferedReader br = new BufferedReader(new FileReader(cacheFile));
                String tempo;
                while((tempo = br.readLine()) != null){
                    Log.i("TEST","Reading from cache "+tempo);
                    if (tempo.contains("http")) {
                        musicUrl.add(tempo);
                    }
                    else {
                        myDataList.add(tempo);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } 
        }
        else {
            try {
                Log.i("TEST", "Creating the cache ? " + cacheFile.createNewFile() + " in " + getFilesDir());
                writer = new FileWriter(cacheFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

What other option I've tried: Android File on Internal Storage not found

Upvotes: 0

Views: 78

Answers (1)

Matt Clark
Matt Clark

Reputation: 28579

you should use one of the following:

getExternalFilesDir()
getExternalStoargeDir()

the first will create a file in /sdcard/android/app.package/file
the second will be the root of /sdcard/

The call that you currently make, getFilesDir(), is returning the path in /data/data/app.package/file which is not accessable to anything other then your application.

Upvotes: 1

Related Questions