0

I have custom logging feature in my app with following code.

private boolean ready = false;
private File logFile1 = null;
private File logFile2 = null;
private String filePath1,filePath2,filePath3;

public NanoLog(int maxBytes)
{
    try
    {
        String sdcard_status = Environment.getExternalStorageState();
        if(sdcard_status.equals(Environment.MEDIA_MOUNTED) == false)
        {
            return;
        }
        String targetFolder = Environment.getExternalStorageDirectory().toString() + File.separator + "imadoco_log";
        File folder = new File(targetFolder);
        if(folder.exists() == false && folder.mkdir() == false)
        {
            return;
        }
        filePath1 = targetFolder + File.separator + "log1.txt";
        filePath2 = targetFolder + File.separator + "log2.txt";
        filePath3 = targetFolder + File.separator + "log.txt";
        logFile1 = new File(filePath1);
        logFile2 = new File(filePath2);
        if(logFile1.exists() == false)
        {
            logFile1.createNewFile();
        }
        else
        {
            limitBytes(maxBytes);
        }
        ready = true;
    }
    catch(Exception e)
    {
        ACRA.getErrorReporter().handleException(e);
    }
}

public synchronized void put(String text)
{
    if(ready)
    {
        try
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter(logFile1,true));
            bw.append(text);
            bw.newLine();
            bw.close();
        }
        catch(IOException e)
        {
            ACRA.getErrorReporter().handleException(e);
            ready = false;
        }
    }
}

Class NanoLog is used to write debug traces to file. Important part of above code is to prepare folder and write file.

My app is getting exceptions shown below with some phones. Most of phones work as I expected, so I believe issue is not caused by permissions in manifest file.

  • java.io.FileNotFoundException: /mnt/sdcard/imadoco_log/log1.txt (Permission denied)
  • java.io.FileNotFoundException: /storage/sdcard0/imadoco_log/log1.txt: open failed: EROFS (Read-only file system)
  • java.io.FileNotFoundException: /storage/emulated/0/imadoco_log/log1.txt: open failed: EACCES (Permission denied)

To make thing clear, I have this permission.

android.permission.WRITE_EXTERNAL_STORAGE

I got them with ACRA's crash report. They are caught on next line of code.

BufferedWriter bw = new BufferedWriter(new FileWriter(logFile1,true));

I can't understand why some phones are getting those exceptions. In case some error is detected in constructor, function put() won't try to write file.

Any idea?

Tomcat
  • 1,405
  • 3
  • 22
  • 37
  • I believe that you should also check for sd card status in `put` method, not just in constructor. Consider how much time passes between object instantiation and actual call of `put` method and what can happen in a meantime? Also take a look a this: http://stackoverflow.com/questions/15711098/trying-to-create-a-file-in-android-open-failed-erofs-read-only-file-system – Dusan Apr 30 '15 at 13:30
  • @Dusan, Yes I need to consider that situation. But I'm not sure doing so prevent this issue from happening or not. – Tomcat May 01 '15 at 06:31

1 Answers1

0

You should not only check if the folder where you want to write in exists but also if this is writable. Use File:canWrite() on it.

logFile1.createNewFile();

You are not checking the return value. So you dont know if it succeeded.

But anyhow you better do not use that statement. The new Filewriter will care for that.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I thought if logFile1.createNewFile() fails then it throws IOException but document says it does not all the time, so I'd better check it. And I will and File#canWrite() check and see what happens. – Tomcat May 01 '15 at 06:41