Working with SDCard’s filesystem in Android

It is very easy to work with SDCard in Android. Here we are working with File objects as with standard Java I/O operations.
In this article I am going to show how to read,write,create files and how to create a folder in SDCard.

Let’s look from the beginning.

Firstly need to add permission to our AndroidManifest.xml file

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

File name. Here we are using System.currentTimeMillis(),because we want to have different file names

String fileName = "test-" + System.currentTimeMillis() + ".txt";

Next step is to get root folder in SDCard,and here we need to use

File sdDir = new File(Environment.getExternalStorageDirectory().getPath());

Next need to check if we can write data to SDCard and after that we will create folder there

File testDir = new File(sdDir.getAbsolutePath() + "/test_folder");testDir.mkdir();

After that need to write data to our file

fos = new FileOutputStream(file);fos.write("Hello,World!".getBytes());

Don’t forget to close stream

finally{if (fos != null){try{fos.flush();fos.close()} catch (IOException e){}}}

At this point we created folder and wrote data to our file which is inside this folder.

 

Need to read this data. It is also very easy.
At first need to get file which we want to read

File readFile = new File(sdDir.getPath() + "/test_folder/" + fileName);

Next we read this data,put it on our TextView component and close stream

FileInputStream fis = null;try{fis = new FileInputStream(readFile);byte[] reader = new byte[fis.available()];while (fis.read(reader) != -1){}readOutput.setText(new String(reader))} catch (IOException e){Log.e(APP_TAG,e.getMessage(),e)} finally{if (fis != null){try{fis.close()} catch (IOException e){}}}

As I said previously working with SDCard filesystem is very simple. At this time I didn’t meet phone which doesn’t work with SDCard,these cards are very important. That’s why the knowledge of how to work with SDCard in Android is very important. I hope that my simple article will be useful for you.

And last…full example:

import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.widget.TextView;public class Main extends Activity{private static final String APP_TAG = "tag";private TextView readOutput;@Overridepublic void onCreate(final Bundle icicle){super.onCreate(icicle);this.setContentView(R.layout.main);readOutput = (TextView) findViewById(R.id.output);String fileName = "test-" + System.currentTimeMillis() + ".txt";// create /sdcard/test_folderFile sdDir = new File(Environment.getExternalStorageDirectory().getPath());if (sdDir.exists() &&sdDir.canWrite()){File testDir = new File(sdDir.getAbsolutePath() + "/test_folder");testDir.mkdir();if (testDir.exists() &&testDir.canWrite()){File file = new File(testDir.getAbsolutePath() + "/" + fileName);try{file.createNewFile()} catch (IOException e){Log.e(APP_TAG,"error creating file",e)}if (file.exists() &&file.canWrite()){FileOutputStream fos = null;try{fos = new FileOutputStream(file);fos.write("Hello,World!".getBytes())} catch (FileNotFoundException e){Log.e(APP_TAG,"ERROR",e)} catch (IOException e){Log.e(APP_TAG,"ERROR",e)} finally{if (fos != null){try{fos.flush();fos.close()} catch (IOException e){}}}} else{Log.e(APP_TAG,"error writing to file")}} else{Log.e(APP_TAG,"ERROR,unable to write to /sdcard/test_folder")}} else{Log.e(APP_TAG,"ERROR,/sdcard path not available")}// Read file blockFile readFile = new File(sdDir.getPath() + "/test_folder/" + fileName);if (readFile.exists() &&readFile.canRead()){FileInputStream fis = null;try{fis = new FileInputStream(readFile);byte[] reader = new byte[fis.available()];while (fis.read(reader) != -1){}readOutput.setText(new String(reader))} catch (IOException e){Log.e(APP_TAG,e.getMessage(),e)} finally{if (fis != null){try{fis.close()} catch (IOException e){}}}} else{readOutput.setText("Unable to read/write sdcard file,see logcat output")}}}

Download code from this article.

 

If you’d like to get the latest posts as soon as they’re published,subscribe to our feed!

Share

No related posts.

2 comments to Working with SDCard’s filesystem in Android

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.