Android file system explained will take a look at the many options that we have today! We will continue to add features and improve our file manager app. I am running it now in order for us to see what is missing, what we want to change and so on. The first thing we see when we start the app is the download folder which we are using as the entry point for our application. The first one we see is the B which means back, go back which allows us to go back to our previous folder through the parent element. We can also reload and create a new folder.
Android File System Explained
If you will enjoy reading and contributing to the discussion for this post, will you please join us on the YouTube video above and leave a comment there because I read and respond to most comments on YouTube? If you find anything helpful in this video or funny, will you please leave a like because you will feel great helping other people find it?
For example, here we will create a folder we will be calling folder one, for example. Great. Now, we have here a folder one. If we click on it, we open the folder which is great. Now, we want to keep adding more features to your file manager. We want to be able to rename files. When we long click one item, we want it to show some options for us to perform. We will run it again and we will see what happens when we long click something. It says, “File manager keeps stopping.” Here we will find the error that is causing just that. Here’s the error which is “Attempt to get length of a null array.” The null pointer exception.
Right now, in order for us to fix this problem, we have to make sure we are not accessing a null object. In this case, the error is here in line 180 in my case. Files is the one that we actually– We don’t have the length because files is null right now. Why files is null? That’s a good question. If we see files right here, we see that we are initializing to dir list files. What we should do here instead is to allocate files memory and then we go files, new. Files is a file array so we’re going to create a file array. The size of the array will be dir list files, list and maybe length either correct.
List length or just list files. I think we will use list files and then length. List files and then we use length. Now, we will run the app again and see if everything works fine. We are just finding ways in we can we improve our application. We are doing it on the go. If we long click this now, the app keeps crashing so what’s the problem? Maybe we have different problem, maybe we have the same problem. Let’s see right here what’s the problem. If you go to your logcat and see, again, we say we have a null array so the dir list files is actually a null array. We did this incorrectly. The dir, the directory is a null array. The first thing we have to do is to make sure we don’t have a null array because directory is set to current path.
Right here in line 156, as you can see and then files right here is set to dir.listfiles. When we go down here where we call the refresh button in this line when it says dir list files, directory is not being updated correctly. What can we do to fix the problem? First, we have to understand that this dir file, we have to check if dir is null. If dir equals null, we will return. We are debugging right now to see– We are running the app again and we see if we long click this, it still crashes. What’s going on here? Logcat, we will fix it right now. Let’s see. Again, we still have the same thing. This is a null array. Actually, files is what we have to test. Actually, dir.listfiles, actually, but we can do this more efficiently by checking just files later. After here, we will say, “If files is null, we will return.” We run the app and we checked everything works fine. We long click on something and at least it doesn’t crash which is great. Later, we will figure out everything we need to do.
For now, we will try to clean a bit the code. We can expand the imports to see what we’re using. Here and the on Create method which is the first method that it’s called we’re setting the content view. After this, we will move the onPause method up there because that’s the next method that it’s called. We are making everything look better. Otherwise, it will be messed and we don’t want that. We go and move the whole onResume method which is big right now, as you can see. We will move that method just after the onCreate method. It will be the second method that we have on our source file. In this way, we can see more clearly what’s going on with our application. First, you can see that we set the content view to the layout that layout one. Next, we go into onResume method and we actually check if we have to request permissions which is already done and works great. Now, here we check if the file manager is initialized or not.
Now, as you can see this is a variable. We will go to where the variables are defined outside the onResume method and we will also move them. Here are some variables. These are permission variables so we will ignore them for now.
We will move the rest of the variables that we can find right here. We move this to the top just before the onResume method. In this way, we will use the variables or define the variables just before they are used. The first thing we do is we check if the file manager is initialized. A private boolean as a global variable doesn’t need to be initialized to false because it’s false by default. Java automatically initialized it.
Now, current path. The current path, we are setting the current path to download and the root path, we’re setting it to the root. Right now, the directory is a file. The directory is a file that is defined by the current path, and files is a file array in which the current files are stored, the files that are shown on the screen. We have the path output which shows the path. If we go back to the app right here, when I long click this, the path output was set to a file name, which we don’t want that, right? So we will find the piece of text, the piece of code that is doing this. To do that, we’re going to press Ctrl+F to find. We will type pathoutput.settext. This is the first occurrence and in this case, it finds three matches. The first one is when we initialize this, which is because that’s a must. The second one is when we go back. This is also not the case because we are not using the go back button. The third case is when we do an on item click listener. Here in order to fix this, we are mixing these two elements, or meta code, right here, so we have to fix this problem first.
The problem is when we long click this code which is correct, but also the on item click is called which we don’t want to happen. We want to know when these should be called. To do that we will implement an easy fix right here, and we will create a boolean flag. We’ll go and say private boolean is long click. We create that boolean flag, and we go to our list view on item click listener and on long click listener. In the unlock click listener, we will go and say is long click will be set to true.
We want to, of course, set that to false, but not immediately, so we have to do it slightly later. How do we do that? We create a handler. We go and type new handler that post delayed, which means run this a little bit delayed. We will, of course, end line with semicolon, and inside here we will type new runnable, so we call it new runnable. Inside this, we go and say maybe the app generates these for you. If not then you’re going to type @overwrite and public void run. Inside this run method, we should execute what we want.
After this runnable parameter, we have to pass the parameter to delay in milliseconds as you can see right there. We will delay these maybe 500 milliseconds, or something like that, we’re just testing. What we will do here is we flip the flag right. We’ll say is long click will be set to false which is cool. Why did we do that? Why do we do this? Right here on the on-item click listener, we have to check for the flag to see if the flag is set, but we want to do all of that in a delayed fashion. This means we have to create a new handler again. You’re going to say new handler that post delayed, again, near runnable, and this time ready auto-generated this for us. You have to put the delay right here maybe 200 milliseconds or whatever. Here it will check if the flag is set. We’re going to say if it’s long click, then what we will do is actually return. If it’s long click we will return, or we will not really return. If is long click is false, if the flag is not set, we will paste this code right here. Position here it’s underlined because we have to make it final because we are using the run old class, so go type final position in the parameter. As you can see, the wording is gone.
What is this doing? What these will do is that it will wait 200 milliseconds to see if long click is or not called. We will see if that delay works or not. How do we do it? We have to use an extra check right here because if you notice, these four lines are just okay in the case of a directory. We have to check if the current file, this directory in order to run this. We’re going to say if files at position is directory, and we will, of course, run this, because as you can see we are referring it’s only two directories right, and now we will run the application and see if everything went fine.
If we just single click on anything that it’s not a directory, nothing happens which is great. But if we click on a directory, like this folder one right here, folder one is opened, which is also great. Now if we long click in folder one, it’s also open if you long click long enough. This is where delays we are talking about comes in place. We have to have a delay that match the user interaction. If long click is there, we have to unset the long click after a larger period of time.
Instead of 500 down here, we will put 2,000, for example, and we will run the app again and check if this works or not.
If we long click folder, nothing happens as you can see. Only when we do a single click, folder is opened. If we long click, it is just selected. This time is also opened. What can we do different here? Here we can maybe check again, and long click. It’s been opened which is not cool. Just to make sure we will run the app again in order to know if we are not messing with flags. We long click a folder it just selected. We long click again, and it opens. Something is wrong with the flags right here.
The flags are not okay. How do we fix this? 200 milliseconds, maybe you have to increase this to maybe 500 milliseconds, that would be kind of better. If long click is as default false, so it’s not set. But when we long clicked an item, it is set to true. This should be called– Let’s go and see. Folder one long click, it’s just long click again, it’s unselected. Long click again is selected and goes in. We still have a bug here. Long click, select it, and in. The first time works fine, but then it’s not working fine. Why? Because we are setting maybe this thing to small delay here, maybe a thousand would be better.
I know what’s going on here. Let’s run and see if this works. Long click on folder right now. Select it, and click, long click, long click, long click, long click. Now it’s kind of working as we expect. We can leave it as it is right now because it’s working great. If we do single click, it opens and we have a crash. It’s actually good having crashes because we can fix those problems. Right now it’s an array index out of bounds exception. We’ll go and identify these right now.
In long click, the position is nine and the length is actually zero. Why is this a problem? Actually, because of the delay we’re having right here. Here if position is greater than files that length, we have to return. Check if position is greater than files that length, we will just return right here. Let’s see what. It works now. We’ll long click a folder, long click again, long click again, long click again, everything works fine, but it’s still not working that well, it has still some errors.
We can improve these by changing the delays we are using here. For example, the long click we will test how fast the long click is called. We will go these down to 100 milliseconds, and these down here to 100 milliseconds, and see if it either still works. If it works, then better for us. If not, we have to find different one. Long click, works. Long click, works. Long click, works. Long click, works. Long click, works. Now it’s even more stable than previously but still not that stable.
To make this more stable, we have to decrease these right here. We have to decrease these right here to maybe 800 or 500. We will extensively test in order for us to make sure everything works fine. Long click, and it’s not working. Long click, long click. Problem is not there apparently. We will leave these as 500 maybe, or maybe we will increase it to 1,000, but we will decrease this a hundred milliseconds to maybe 50 milliseconds. Let’s now see if everything works. Folder, long click, long click, long click. It works as we want which is awesome. It’s working great.
Now, if you go back, we have different folders. Here we can create a new folder. Inside music, for example, my music folder and we press okay, and here we have my music folder. If we go inside my music folder, we can create another folder. For example, my music folder two. If we go back, we’ll be in music and out here. Music, my music folder. I think we found another bug. Back, back, back. Music, my music folder, folder two. Back. Now I think it works fine actually. Yes, it’s working great. We have done that and we have now fixed a couple of bugs we were having. I hope you enjoyed the video and I see you in the next one. Bye.
Thank you very much for finishing this tutorial. What you’ve watched is a part of the full course named Code and Android File Manager and Explorer App. You can get here. You can also just go to Uthena and buy it. The very best value is if you want this entire course and you’re learning to be an Android or iOS developer, you can see this course has something like 12 videos in it, but you can get the entire Android mobile app developer bundle for life, you can get this one bundle for $48 here. This is linked in the description also.
The idea with this bundle is you buy this bundle once and all the courses we add for life are included in it. Currently, there’s 25 plus hours of videos from 2019. I imagine over the next eight months we will add 75 plus more hours. We have multiple developers working almost every day to film new videos for you to give you an outstanding experience and one place you can go to from a total beginner to having an outstanding career as a developer online.
Thank you for watching this with us. We appreciate the chance to share educational content with you for free every day that we hope is helpful. You buying the bundle or the course helps us to pay the developers to give you even more.
Will you please leave a like to let us know you made it this far in the video? Because, you will feel great about helping the video rank higher and giving something back.
Get private label rights to sell this course yourself as a partner!
Thank you for making it to the end of this post.
I love you.
You are awesome.