tirenweb
tirenweb

Reputation: 31749

Android: trying to save a file

Im just trying to write a text file using this code:

    try {

        File ins = new File(context.getFilesDir(), "myfile");
        FileOutputStream out = new FileOutputStream(ins);
        out.write(string.toString().getBytes());
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

I have tried also this code:

    File aux = context.getFilesDir();

    String filename = "myfile";
    String string = "Hello world!";
    FileOutputStream outputStream;

    try {
        outputStream = openFileOutput(aux + filename, MODE_PRIVATE);
        outputStream.write(string.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

The problem: Im not getting any error, but none of them are saving a file. I expected to find the new file at /data/data/com.example.tirengarfio.myapplication/files, since the output of content.getFilesDir() is that.

Moreover, I can not find a /data/ folder at root but an /Android/data one, and inside of the latter, there are several directories like com.android.browser or com.dropbox.android, but none of them is named com.example.tirengarfio.myapplication.

Upvotes: 0

Views: 78

Answers (2)

Blackbelt
Blackbelt

Reputation: 157487

you need root permission to access /data/data from adb.

but an /Android/data one, and inside of the latter, there are several directories like com.android.browser or com.dropbox.android, but none of them is named com.example.tirengarfio.myapplication

use getExternalFilesDir if you want to write in that directory. You need to add the write permission on the manifest

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007584

I have tried also this code:

That will not work, as aux + filename is invalid, and your code will trigger some sort of IOException. Your first code snippet is better.

I can not find a /data/ folder at root but an /Android/data one

That is because you are looking in external storage, not internal storage. Internal storage is inaccessible on production hardware, but can be browsed on an emulator using DDMS's file explorer.

Upvotes: 1

Related Questions