How to Get Absolute Paths of Internal and External Storages in Android Development

Nitish Kumar Singh

Nov 24, 2023 . 2 min read

Hello developers! In this blog post, we will learn how to get the absolute paths of Internal (Phone storage) and External (SD Card) storages in Android app development using Java.

Photo by William Hook on Unsplash

When developing a File Manager, Video Player, Music Player, or any app where we need to list all or a specific type of files, we need to retrieve paths of Internal and External storages.

We know that the path of Internal storage is /storage/emulated/0, and External storage is /storage/name_of_sd_card in almost all devices. Here, name_of_sd_card is a variable because it is not the same for all SD Cards. Using these hard-coded paths is not a good idea.

So, how do we get paths because the Android SDK does not provide any direct methods to obtain paths? We can list files with MediaStore, but here we want to use the File API.

To get paths, we use the ContextCompat.getExternalFilesDirs method. This method returns paths to the files directory (application-specific) on all available external storage devices. The returned paths are not exactly what we want, but we can modify them to suit our needs. See the following lines to understand what the Android Developer Documentation says about this method.

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

The following code shows how we obtain our desired paths:

ArrayList<String> rootPaths = new ArrayList<>();
File[] rootsStorage = ContextCompat.getExternalFilesDirs(context, null);
for (int i = 0; i < rootsStorage.length; i++) {
    String root = rootsStorage[i].getAbsolutePath().replace("/Android/data/" + context.getPackageName() + "/files", "");
    rootPaths.add(root);
}

Here, the first path is for Phone Storage, and the second path, if available, is for the SD Card. The returned paths do not include USB storage. To list a specific type of files, we loop over these files and filter with extensions to get the required files.

I hope you learn how to obtain paths of Internal and External storages and enjoy reading this blog post.

Published on Nov 24, 2023
Comments (undefined)

Read More

How to Listen to URL Changes in Next.js for a Route or Overall App

Hello Developers, In this blog post, we will see how to listen to URL changes at the route level or application level in a Next.js app. We will create a listener to listen for URL changes in both App Router and Pages Router.

Nov 23, 2023

How to Remove All Child Elements at Once in JavaScript

Hello, developers! In this blog post, we will learn how to remove all child elements at once from an HTML element using JavaScript. Removing all children at once is a little problematic task on the web because the web does not have any method like removeAll() that removes all children.

Nov 17, 2023

How to Hover on a Child Element Without Hovering on the Parent, Using Only CSS

Hello, developers! In this blog post, we will learn "How to hover on a child element without hovering on the parent, using only CSS". I mean adding some style to the child element when hovering over it, following the conditions below:

Nov 7, 2023

How to Control Volume using Android SeekBar in Java

Hello Developers! In this blog post, we will learn how to change volume using SeekBar in Android using Java. Basically we increase or decrease system volume of phone when progress of SeekBar chnages and show a TextView for volume level.

Nov 15, 2023

How to Change Brightness of Only One Activity in Android Using Java

Hello Developers! In this blog post, we will learn how to change the brightness of only one activity, not for the overall application or the entire phone brightness. For example, if the user drags on the screen, changes the progress of the seek bar, or clicks on buttons, then the brightness of the activity should change. This is a feature commonly seen in Video-Player Apps.

Nov 13, 2023

How to Convert Milliseconds Time into Formatted String (HH:MM:SS) in Java

Hello Developers! in this blog post, we will learn how to convert milliseconds time into formatted String in Java. For example 6548000 milliseconds into 1:49:08, so we can show it as duration to user.

Nov 12, 2023