0

I am creating a file with this address:

Environment.getExternalStorageDirectory() + File.separator + "file.txt"

I run it on a real device. When I disconnect the device from pc and go to a file explorer program, I can see file in my route. But when I connect phone to pc and go to its drive from my computer, I cannot see the file.

What's the problem?

Thanks

Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57

2 Answers2

1

Use MediaScannerConnection to notify the system of new files and directories.

USB connection to PC uses PTP or MTP protocol and the corresponding databases need to be refreshed before the files can be seen on PC.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Try using this method:

public Boolean write(String fname, String fcontent){
      try {
        String fpath = "/sdcard/"+fname+".txt";
        File file = new File(fpath);
        // If file does not exists, then create it
        if (!file.exists()) {
          file.createNewFile();
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(fcontent);
        bw.close();
        Log.d("Suceess","Sucess");
        return true;
      } catch (IOException e) {
        e.printStackTrace();
        return false;
      } }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
       
   }
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
  • Same problem. I also used File file = new File(Environment.getExternalStorageDirectory(), "file.txt"); Surprisingly I found that when I see file explorer in eclipse, I see my file in mnt>sdcard>X, Where X is the first folder in my phone sd card that its name starts with same letter as file. For example I see file.txt in a folder named "far" or kk.txt in a folder named "kafe". But I cannot see file.txt or kk.txt in my pc even on those folders. – Misagh Emamverdi Sep 04 '14 at 15:14
  • check this official doc for creating file http://developer.android.com/training/basics/data-storage/files.html – Himanshu Agarwal Sep 04 '14 at 15:25