Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Folder on phone not showing in Windows PC

Tags:

android

I am writing an app to create a folder on my Nexus 5 and then write a text file inside the folder.

The above part is working just fine. I am writting using the following code:

Creating the folder:

File sdCard1 = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard1.getAbsolutePath() + "/SmsApp");
        if (dir.isDirectory()) 
        {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) 
            {
                new File(dir, children[i]).delete();
            }
        }

        File sdCard = Environment.getExternalStorageDirectory();
        directory = new File (sdCard.getAbsolutePath() + "/SmsApp");
        directory.mkdirs();

And writing the string to text file.

public void writeToText(String texttosave)
    {
        File sdCard = Environment.getExternalStorageDirectory();
        File logFile = new File(sdCard.getAbsolutePath() + "/SmsApp" + "/smsrawdata.file");
        if (!logFile.exists())
        {
            try
            {
                logFile.createNewFile();
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        try
        {
            BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
            buf.append(texttosave);
            buf.newLine();
            buf.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

Text file is showing up properly on my phone but when I am trying to access the same folder on phone I can't see the folder at all.

I thought it would be the same for other app on play store that creates a folder but then everything gets created and then I can see them on Windows Explorer.

Am I doing something wrong in the code part for it not to show on my Computer but show only on my phone. I am using ES File Explorer to see files on my phone.

Please let me know if someone else is having the same problem.

Thanks!

like image 879
TheDevMan Avatar asked Nov 01 '22 04:11

TheDevMan


1 Answers

Your pc will communicate with the media store about files. Not directly with the sdcard or external memory. If the media store does not know about your file your pc can not see it. You forgot to tell the store that you created a file. For every new file you should invoke the media scanner for that file. You are not the first one who happens this so the problem has been reported many times. You only need to add a few lines of code which you will find easily searching this site for invoking media scanner on new file. If you switch off/on your device the file will be known soon too.

like image 187
greenapps Avatar answered Nov 10 '22 18:11

greenapps