Creating a file on an SD card in Android

I want to create a file on an SD card and then save the CSV file in it.

From surfing around, I noticed that there seem to be two ways around this:

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

If you use API level 8 or more, use getExternalFilesDir () to open a file representing the external storage directory in which you should save your files. This method takes a type parameter that indicates the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (going from null to get the root of your application file).

If you are using API level 7 or lower, use getExternalStorageDirectory () to open the file representing the root of the external storage. Then you should write your details 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");

Which way to use? If the first, how can I write an application for compatibility with the API level <= 7 and> = 8? Is there any good tutorial for this, first of all?

+3
source share
1 answer

Well, if you need compatibility with API level 7 and below, use the method getExternalStorageDirectory(). Also note that when looking at API level 4, you should request permission to write to the SD card:

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

Source: https://habr.com/ru/post/1763311/


All Articles