Christian
Christian

Reputation: 26427

Creating a file on the SD-Card in android

I want to create a file on the SD-Card and later save a CSV file in it.

From surfing around I noticed that there seem to be two ways about going about it:

http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory).

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

/Android/data//files/

And http://www.anddev.org/working_with_files-t115.html :

FileWriter f = new FileWriter("/sdcard/download/possible.txt");

What way should I use? If the first, how do I write my application to be compatible with both API level <=7 and >=8? Is there some good tutorial for doing it the first way?

Upvotes: 3

Views: 5808

Answers (1)

Konstantin Burov
Konstantin Burov

Reputation: 69238

Well if you need compatibility with API level 7 and lower, use getExternalStorageDirectory() method. Also note that staring from API level 4 you have to request permission to write onto SD card:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 6

Related Questions