Home > Android > playing with sdcard in android

playing with sdcard in android

Howdy everyone, today i am going to share a little code snippet that i developed recently while i was playing with my usual android development journey.

I wanted to build an android file manager,where one can browse all the files from sdcard.To bring up the pace in me at first i wanted to see all the files in sdcard.And this post is about that code snippet.

Another thing i want to ask that did you familiarize yourself with ddms? If not, that’s bad bcz i am sure of that if something goes wrong in your application you probably thinking your brains out.I tell you, ddms is big big helper when the whole world is beyond your hand. 🙂

In this post i am going to use ddms too, to help me out what’s going wrong.

So, let’s not talk too much…

        File file[] = Environment.getExternalStorageDirectory().listFiles();
		FilesInDir(file);

Above line is the most mandatory one but not everythig even if it seems.

    public void FilesInDir(File[] file1){
        int i = 0;
        String filePath="";
            while(i!=file1.length){
                filePath = file1[i].getAbsolutePath();
                i++;
                Log.d(i+"", filePath);
            }
        }



Running that code snippet will show us all the files and directory name in ddms right after /mnt/sdcard/
But that’s not what i want.I want to see all the files.And that’s when i got a brilliant idea of recursion, hah maybe that’s why they teach us recursion, DnC and all.To see the world 🙂

So, let’s use it … 🙂

    File file[] = Environment.getExternalStorageDirectory().listFiles();
	recursiveFileFind(file);

    public void recursiveFileFind(File[] file1){
    int i = 0;
    String filePath="";
        while(i!=file1.length){
            filePath = file1[i].getAbsolutePath();
        	if(file1[i].isDirectory()){
        		File file[] = file1[i].listFiles();
                recursiveFileFind(file);
        	}
            i++;
            Log.d(i+"", filePath);
        }
    }

After running this we will see a "Force Close" in our emulator.

Now, what to do?Swithing to ddms perspective view we will see lots of red light:

but never fear this is where our solution is reside.

Let’s examine what is the problem:

you can see it’s a NullPointerException which is causing the problem, that means we are assigning a null reference somewhere in our code.

Closely look at the recursion:

what happens when there is only a directory left at the end of a folder but no files in it.

So that means, at that time we are assigning what’s in this below line? Which has no length!!!

		File file[] = file1[i].listFiles();

So, to fix it all we need to do is check to see if the reference is null?

	if(file1!=null){
		while(...){
		....
		}
	}

that should do it.

See how ddms helped to us to make a way when we can’t figure out things all by ourself.

That’s it for today, next i will try to write about Sensor bcz i recently added the Samsung Sensor Simulator in my eclipse, which is awesome.

Advertisements
Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: