1

I'm creating a directory and a text file on the sdcard in one of my apps because I want to be able to move it to my computer for analysis. But I can't find the folder or the file I'm creating on my sdcard using the file browser on my computer. I CAN find and read the file using my phones file manager but not using the file browser in windows. So the file and folder are succesfully created and I can write to the file, I can also find and read the file using the file manager on my phone but I can't find either directory or file using my computer.

I have a uses permission for the application to allow it to write to external storage.

This is the code I use to create the file and directory.

String fileName = "testFil.txt";
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/PulsApp";

File appDirectory = new File(path);
appDirectory.mkdirs();
File file = new File(path, fileName);

try {
    file.createNewFile();
} catch (IOException e) {
}

Does anyone know what the problem is and how to fix it? I really need to be able to write files to my sdcard so I can transfer them to my computer.

I am completely baffled by this problem since all the research I've done point to that everyone else is doing the same thing.

mpals
  • 121
  • 3
  • 15
  • you're doing it right, make sure you have access to the right SDCard in windows. Can you see the file in adb shell or can you pull it with adb instead of windows explorer? – bbedward Mar 22 '12 at 12:02
  • This isn't really a solution to your problem, but never use string concatenation for paths. Use Path.Combine() - http://stackoverflow.com/questions/412380/combine-paths-in-java – Stealth Rabbi Mar 22 '12 at 12:03
  • @bbedward Ok, I tried switching to ubuntu, but I still couldn't find the files just plugging in my phone with the usb cable. So in a fit of rage I tore apart my phone and put the sdcard in a usb adapter AND then I could find it. Does this have something to do with permissions or something like that? – mpals Mar 22 '12 at 13:12

2 Answers2

6

If your device is running Android 3.0 or higher, you also need to use MediaScannerConnection to index your newly-created file before it will show up on a development PC's file explorer.

More accurately, the newly-created file needs to be indexed by the MediaStore. That will eventually happen for other reasons (e.g., device reboot). However, you are better served using scanFile() on MediaScannerConnection to get it to happen more quickly.

I blogged about this last summer.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Sometimes that the MediaScannerConnection will recognize the folder as a unknown type file, so try to create another folder inside the original one can avoid this problem. I have met the same problem, and I use the method in the comment

And it works for me.

Community
  • 1
  • 1
mtyong
  • 121
  • 1
  • 3
  • 13