Monday, March 24, 2014

How to Create a File on the SD Card With Android Developer



1. Open your Android application's source code file with a text or programming editor.

2. Browse to the location in the source code where you wish to call the function that writes a file to the device's external storage.

3. Insert this single line of code to check for the SD card:File sdCard = Environment.getExternalStorageDirectory();

4. Insert these lines of code to set the directory and file name:File dir = new File (sdcard.getAbsolutePath() + '/folder1/folder2');

dir.mkdirs();

File file = new File(dir, 'example_file');

5. Replace '/folder1/folder2' in the above code with the actual path where you intend to save the file. This should be a location in which you normally save your application files. Also, change the 'example_file' value to the actual file name you wish to use.

6. Insert the following line of code to output the file to the SD card:FileOutputStream f = new FileOutputStream(file);

7. Save the file, then compile it and test the application using the Android emulator software or the device.

No comments:

Post a Comment