Introduction
This tutorial series will guide you through building a simple Android File Explorer application in Kotlin. You will develop a file explorer application with core functionalities of creating, deleting, copying and moving files/folders. I assume that you have a good understanding of Android Development and Kotlin language. These tutorials are not meant to teach you Kotlin or Android. Think of this series as practicing/refreshing your Kotlin/Android skills. We will not be focusing on making the best file explorer application or the best architecture application ever! As I said this series is meant for practicing and refreshing your skills.
Brief breakdown of upcoming tutorials
I have divided this series in segments of short to medium sized tutorials. In this tutorial we will be setting up our project. In next tutorial you will build the core part of the application which is browsing through the file system structure. Then you will follow up by adding breadcrumbs to provide navigation to any point previously opened in the file structure. After that you will add feature to create/delete files and folders. Followed by that you will add options to copy and move files from one directory to another. Finally we will end the series with a tutorial on miscellaneous stuff!
Throughout the tutorial I will not be providing the complete code, as I expect you have a fair knowledge of Android Development, code that is too verbose will not be shown. The complete code for the project we are building is available on this GitHub repo. I recommend that you download the complete project, in case you get stuck at any point, you can refer the completed project.
Setting up the project
Make sure you have the latest Android Studio version installed. Open it and create a new project, make sure to turn on Kotlin support.
Once the project is created, add the permission for accessing external storage.
1 2 |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
As of Android 6.0, permissions have to be request from the user at runtime, you will do that in the next tutorial.
Add a FileProvider
From Android 7.0 your app is not allowed to share a ‘file:///’ URI with an Intent, we would require that to open files when when the user taps on them. We can fix this behaviour by proving a FileProvider and then getting a shareable Uri for our ‘file:///’ path. Right now you will just set up the file provider, you will use it to launch intents in next tutorial.
Note: FileProvider was introduced for secure sharing of files across applications. if you want to learn more read the official doc on FileProvider.
Go ahead and create a class named GenericFileProvider which extends FileProvider.
1 |
class GenericFileProvider: FileProvider() |
Create a file in the xml directory named provider_paths.xml.
1 2 3 4 5 |
<paths xmlns:android="http://schemas.android.com/apk/res-auto"> <external-path name="external_files" path="."/> </paths> |
Register GenericFileProvider in manifest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".main.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:authorities="com.thetechnocafe.gurleensethi.kotlinfileexplorer" android:name=".GenericFileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider> </application> |
That is it for the setup, next you will dive deep into the code.
Screenshots of Completed Kotlin application
Here is a small tutorial series on Kotlin language.
2 Comments
Viktor · October 13, 2020 at 2:54 pm
Good tutorial, but it won’t work on Android 10+. Is there anything we can do with that?
Build a File Explorer in Kotlin – Part 2 – Reading files from paths - TheTechnoCafe · August 12, 2018 at 8:31 am
[…] So now that you have set up the project, lets start coding and making this File Explorer come to life! (If you haven’t read the first part of this series, please read it here.) […]