Saving to device with no SD card
My friend and I are attempting to create an app that saves files to a device. We used this code to write to an external SD card, and it works great on his Droid X and Samsung Galaxy Tab.
Get the path to the SD card:
private static final File ROOT = Environment.getExternalStorageDirectory();
Create the folder path and files:
FileWriter fw = new FileWriter(ROOT + "/test/" + "time_frames.txt");
we are using document factory to create the documents
so you can see that we create the path then try to save to that path that was just created
File file = 开发者_运维知识库new File(ROOT + "/test/" + "time_frames.txt");
When I run it on my Nexus S (which does NOT have a SD card) is having trouble with the exact same code.
private static final File ROOTtest = Environment.getExternalStorageDirectory();
this returns /data
private static final File ROOT = Environment.getRootDirectory();
this returns /mnt/sdcard
private static final File intData = Environment.getDataDirectory();
this returns /system
my question is which one of these will work for devices that have SD cards and no SD cards? I have tried a lot, but trying all this stuff has really confused me. Thanks in advance
Environment.getExternalStorageDirectory()
returns the path to external storage, it should work on all devices. Whether they have an actual SD card doesn't matter, and your code shouldn't care either. You need to make sure that external storage is available before you try to use it though, because it could be unmounted at any time.
精彩评论