This is my question – How do you get the internal storage absolute path for the Android device in Kotlin Android Studio?
And I have searched the internet and found var gpath = Environment.getStorageDirectory().absolutePath
Ok, but that gives the SDCard storage.
And I have scanned the available methods in the Environment package. There are some that looked promising, like Environment.getRootDirectory()
But that only returns /storage
And even if I try .listFiles()
on that it gives nothing.
I am just new to all this, so maybe there is a simple way to get there that I am missing.
3
Answers
you can use the ContextCompat.getExternalFilesDirs() method to get the paths to the external storage directories that your app can access. This method returns an array of File objects, each representing a storage location. The first element in this array is usually the phone’s internal storage, and the second (if available) represents the SD card storage.
However, the paths returned by this method are application-specific and include the path /Android/data/[your_package_name]/files at the end. If you want the root storage paths instead, you need to remove this part from the path.
Here’s a breakdown of the code you shared:
1.Create an ArrayList called rootPaths to store the final paths.
2.Use ContextCompat.getExternalFilesDirs(context, null) to get an array of storage directories.
3.Loop through each directory, and for each path, remove /Android/data/[your_package_name]/files using the replace() method.
4.Add the cleaned-up paths to rootPaths.
ArrayList rootPaths = new ArrayList<>();
File[] rootsStorage = ContextCompat.getExternalFilesDirs(context, null);
rootPaths now contains the cleaned-up paths.
The first path is for the phone’s internal storage, and the second one (if available) is for the SD card.
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.
The following code shows how we obtain our desired paths:
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.
Internal storage on Android is private to the app, so other apps cannot access this data. To get the absolute path of internal storage in Android using Kotlin, you can use the
context
to retrieve the internal storage directory.To get the internal storage directory using:
The context.filesDir refers to the app-specific internal storage directory.
Android provides several methods through Context and Environment, if you’re looking for different directories within internal storage.
For Files,
val internalFilesDir = context.filesDir.absolutePath
For Cache,
val internalCacheDir = context.cacheDir.absolutePath
To access specific directories like Pictures, Music etc.
You shall use getExternalFilesDir() which stores files in app-specific directories accessible by the user
val picturesDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.absolutePath
The getExternalStorageDirectory() refers to external storage (SD card or emulated storage). The Internal Storage, App-specific, private, and cleared when the app is uninstalled. Also System-wide internal storage, have no direct access due to security restrictions.