2

First of all here's the code so everybody knows what we're dealing with:

try
    {
        // now save to external files
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            String fileName = CONSTANTS.SETTINGS.MANAGER.DEFAULT_FILE_NAME + "." + this.format.value();
            File saveDir = new File(CONSTANTS.SETTINGS.MANAGER.EXTERNAL_PUBLIC_DIR + File.separator + this.projectName);
            if (saveDir.mkdirs() || saveDir.isDirectory())
            {
                File saveFile = new File(saveDir.getAbsolutePath() + File.separator + fileName);
                File saveFileRoot = new File(CONSTANTS.SETTINGS.MANAGER.EXTERNAL_PUBLIC_DIR + File.separator  + "Survey-" + projectName + ".xml");
                LayerManager.getInstance().setDate(new Date());

                switch (format)
                {
                    case XML:
                    {
                        if (CONSTANTS.DEBUGGING_MODE)
                        {
                            SLog.d(CONSTANTS.SETTINGS.MANAGER.LOGTAG, "Writing user file into " + saveFile.getAbsolutePath());
                        }
                        xmlSerializer.write(LayerManager.getInstance(), saveFile);
                        xmlSerializer.write(LayerManager.getInstance(), saveFileRoot);
                    }
                    break;
                    case JSON:
                    {
                        FileWriter saveWriter = new FileWriter(saveFile);
                        try
                        {
                            if (CONSTANTS.DEBUGGING_MODE)
                            {
                                SLog.d(CONSTANTS.SETTINGS.MANAGER.LOGTAG, "Writing user file into " + saveFile.getAbsolutePath());
                            }
                            String content = gson.toJson(LayerManager.getInstance());
                            saveWriter.write(content);
                        }
                        finally
                        {
                            saveWriter.flush();
                            saveWriter.close();
                        }
                    }
                    break;
                    default:
                    {
                        SLog.e(CONSTANTS.ACTIVITY.MAIN.LOGTAG, "No user save requested.");
                    }
                    break;
                }

                // send event to update media scanner
                FileSaved event = new FileSaved();
                EventBus.getDefault().post(event);
            }
            else
            {
                SLog.e(CONSTANTS.ACTIVITY.MAIN.LOGTAG, "Could not write into save projectName");
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return true;
}

This works perfectly on an android 4.1.2 device API 16. The code creates a new diretory, named any value projectName holds, in the "external_public_dir" <= saveDir
Then, depending on format xml/json, in both cases the file is written in the newly created directory. As a test the file is also directly written to external_public_dir. windows explorer screenshot of directory on Android 4.1.2 device

When you attach the device to your (windows) computer the files/directories are where they should be.

However, when this code is run on a device with Android 4.4.4 API 19 the two directories are not recognized anymore by windows... .

windows explorer screenshot of directory on Android 4.4.4 device

The two 'files' test & test2 should be folders containing an xml file each. And the following makes the problem even more confusing: 1)The Android File explorer DOES recognize the directories... 2)As you can see, the other file created by saveFileRoot does appear in windows explorer...

I am stumped, any suggestions or tips would be very helpful!

Thanks in advance

Community
  • 1
  • 1
WiZarD
  • 361
  • 2
  • 5
  • `CONSTANTS.SETTINGS.MANAGER.EXTERNAL_PUBLIC_DIR`. Well what is that? How can that be a constand if that path is different among devices? – greenapps Nov 25 '14 at 23:23
  • `if (saveDir.mkdirs() || saveDir.isDirectory())`. That sequence is wrong. Better: if(!saveDir.getParentFile().canWrite()){Toast(saveDir.getParent() ..not writable..); return false;} if (!saveDir.exists()) saveDir.mkdirs(); if(!saveDir.exist()){Toast(saveDir.getAbsolutePath() does not exist); return false;} – greenapps Nov 25 '14 at 23:30
  • After some further testing I can only conclude that this problem is a device specific problem of the NEXUS device and is related to this post: http://stackoverflow.com/questions/22366217/cant-create-folder-on-external-storage-on-android – WiZarD Nov 26 '14 at 13:51

0 Answers0