Saving public files in android but folders are not

2019-08-21 08:13发布

问题:

I am trying to create some public folders for my app to save some data. My goal is to create a main folder and a subfolder named based on the current date in order to save my data. The data are supposed to be accessible via PC. The problem is the while everything is working when I access the files via the phone[Image 1] (with Bluetooth File Transfer app) the pc fails to recognize the subfolder[Image 2].

[Image 1] All working on mobile

[Image 2] Subfolder not recognized on PC

I have looked at many answers, tried a lot but haven't found a solution. My code is:

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.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS), "Main folder/"+date);
        if (!root.exists()) {
            root.mkdirs();
        }

        root.setExecutable(true);
        root.setReadable(true);
        root.setWritable(true);
        MediaScannerConnection.scanFile(context, new String[] { root.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                    }
                });

        File myfile = new File(root, sFileName);
        writer = new FileWriter(myfile);
        writer.append("Unit\n\n");
        writer.append("#\tValue \tX \tY \tZ \tTime\n\n");
        readyToRecord=true;
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "Error creating file", Toast.LENGTH_SHORT).show();
        stop();
    }
}

I added the following code because I read it would help

root.setExecutable(true);
        root.setReadable(true);
        root.setWritable(true);
        MediaScannerConnection.scanFile(context, new String[] { root.toString() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                    }
                });

I don't include my other functions because they are not responsible for creating the path and file. I hope you can help me. Thank you

回答1:

Scan myfile, not root, and only after you have written to and closed your FileWriter.



回答2:

I found a solution. I change the code to

        File root = new File(Environment.getExternalStorageDirectory()+ "/Main folder/"+date);
        if (!root.exists()) {
            root.mkdirs();
        }
        myfile = new File(root.getAbsolutePath(), sFileName);
        writer = new FileWriter(myfile);

The use of getExternalStorageDirectory works on all android versions unlike getExternalStoragePublicDirectory( Environment.DIRECTORY_DOCUMENTS)

Also the part from CommonsWare answer is necessary.