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?