0

I have some code supposed to create some public folders and save files on them. My code works on my Android 7 device but not on 4.1.1 or 5.1.1 (I didn't try on other phones.)

my code is the following

public void createFile() {
    calendar_time = Calendar.getInstance(Locale.getDefault());
    int hour = calendar_time.get(Calendar.HOUR_OF_DAY);
    int minute = calendar_time.get(Calendar.MINUTE);
    int second = calendar_time.get(Calendar.SECOND);
    String time=String.valueOf(hour)+":"+String.valueOf(minute)+":"+String.valueOf(second);
    String sFileName=date+" "+time+" Part "+String.valueOf(file_counter)+".txt";

    try {
        File root = new File(Environment.getExternalStorageDirectory()+ "/Main Folder/"+date);
        if (!root.exists()) {
            root.mkdir();
        }
        myfile = new File(root.getAbsolutePath(), sFileName);
        if (!myfile.exists()) {
            myfile.createNewFile();
        }
        writer = new FileWriter(myfile);
        writer.append("Unit\n\n");
        writer.append("#\tValue \tX \tY \tZ \tTime\n\n");

        Toast.makeText(context, "Recording...", Toast.LENGTH_LONG).show();
        readyToRecord=true;
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "Error creating file", Toast.LENGTH_SHORT).show();
        stop();
    }
}

On android 7 this works flawlessly. On lower versions though it doesn't. If I include the line myfile.createNewFile(); I get the following error

07-10 23:24:12.307 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err: java.io.IOException: open failed: ENOENT (No such file or directory)
07-10 23:24:12.307 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.io.File.createNewFile(File.java:941)
07-10 23:24:12.307 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.Recorder.createFile(Recorder.java:91)
07-10 23:24:12.307 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.Recorder.startRecording(Recorder.java:67)
07-10 23:24:12.307 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.MainActivity.getPermission(MainActivity.java:97)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.MainActivity.access$100(MainActivity.java:33)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.MainActivity$1.onClick(MainActivity.java:63)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.view.View.performClick(View.java:4856)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.view.View$PerformClick.run(View.java:19956)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.os.Looper.loop(Looper.java:211)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5389)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at libcore.io.Posix.open(Native Method)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.io.File.createNewFile(File.java:934)
07-10 23:24:12.308 6888-6888/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     ... 15 more

If I don't include the line myfile.createNewFile(); I get the following different error

07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Ultimate EMF Detector/10-Ιουλ-2017/10-Ιουλ-2017 23:26:29 Part 1.txt: open failed: ENOENT (No such file or directory)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:465)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.io.FileWriter.<init>(FileWriter.java:42)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.Recorder.createFile(Recorder.java:92)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.Recorder.startRecording(Recorder.java:67)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.MainActivity.getPermission(MainActivity.java:97)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.MainActivity.access$100(MainActivity.java:33)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at textfiletest.xyz.mreprogramming.textfiletest.MainActivity$1.onClick(MainActivity.java:63)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.view.View.performClick(View.java:4856)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.view.View$PerformClick.run(View.java:19956)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.os.Looper.loop(Looper.java:211)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5389)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
07-10 23:26:29.755 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
07-10 23:26:29.756 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at libcore.io.Posix.open(Native Method)
07-10 23:26:29.756 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
07-10 23:26:29.756 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:451)
07-10 23:26:29.756 9831-9831/textfiletest.xyz.mreprogramming.textfiletest W/System.err:     ... 18 more

Can you help me ? The permissions are handled properly: Manifest and runtime if needed based on android version.

EDIT I changed the code to this

File dir = new File(Environment.getExternalStorageDirectory() + "/Main Folder/"+date);
        dir.mkdirs();
        myfile = new File(dir, sFileName);
        myfile.createNewFile();

This fixed the problem for Android 5.1.1 but android 4.1.1 is still popping the same errors.

mremremre1
  • 1,036
  • 3
  • 16
  • 34

1 Answers1

0

The problem in android 4.1.1 is the character ":" that exists in my file name.

12:51:39 Part 1.txt

Changing it to

12-51-39 Part 1.txt

fixes it in all phones. Sadly it now looks more like a date than time.

mremremre1
  • 1,036
  • 3
  • 16
  • 34