Locations of CSV files in Android
I'm having trouble reading in from a .CSV file in an android app. Mainly a "FileNotFound" error. Where would be the easiest place to put the file, and what would be the address to it. I have been messing around with a few 开发者_JS百科different places but have had no luck.
Can anyone point me in the right direction?
You'll probably want it on the removable storage (SD card). First you'll want to check if the storage is available, then read/write.
The docs have some notes on this: http://developer.android.com/intl/de/guide/topics/data/data-storage.html#filesExternal (Note the API level 8 stuff there, such as getExternalFilesDir, I haven't used that yet, but looks handy, if you can guarantee >=8.)
You can check if storage is there with something like:
public boolean isExternalStorageAvail() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
If it's not there show a Toast (at a minimum) or otherwise let the user know.
Then read it like:
File f = new File(Environment.getExternalStorageDirectory() + "/mycsvlocation/myfile.csv");
Then you can use the regular File methods to check if it's there, it's readable, etc., and then use it.
Also note that the docs recommend you use a location on the SD card like "Android/data//files", and that's a reasonable recommendation, but many apps just use "/appname/" or such.
And -- one more thing, you can put the file there just by mounting the SD card and creating that structure and copying a file over, of course. Alternatively, you could do that programatically (from your app) as well, but it sounds like you already have the file, and just want to read it.
精彩评论