Question

In my android app, i am logging errors in a txt file. But when i try to create file , i face an error Java.io.IOException: open failed: EROS( Read Only file system error). I have added write permissions in AndroidManifest.xml but no difference. How to fix it ?

code

    try {       
          File file =new File("Log.txt");
            if(!file.exists()){
                file.createNewFile();
        } 
        catch (Exception e) 
        {
            Log.w("tun tun", e.toString());
        }
Was it helpful?

Solution

Do this way

To store on external storage(SDCARD)

try {
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Log.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            Log.w("tun tun", e.toString());
        }

To store In internal storage

try {
        File file = new File(context.getCacheDir() + File.separator + "Log.txt");

    if (!file.exists()) {
        file.createNewFile();
    }
} catch (Exception e) {
    Log.w("tun tun", e.toString());
}

OTHER TIPS

try some below codes:-

Problem is you are not adding path.

String filePath = context.getFilesDir().getPath().toString() + "/fileName.txt";
File f = new File(filePath);

or

String FILENAME = "hello_file";
String string = "hello world!";

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

refrence

or

path = Environment.getExternalStorageDirectory();
File file = new File(path, "/" + fname);

Data directory has no read/write permission in Android

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top