Media Files Handling In Android Application Development

Within this post, we will present a discussion on how to work with media type files during the process of android mobile application development.

As we know that, android development environment offers an advanced set of tools to develop our desired mobile application from simplest one to complex application for user sensing, media streaming, medical health, game development, food industry etc. Android file handling system allows you to work more easily with different files and let you open them in app of your choice. Android development have been changed a lot after the launch of android 10 as security wise which is such a good thing. With android’s 10 scoped storage, it forces android developers to use storage access framework for all media file operations within app. Things have become more easy and restrictive with the use of scoped storage feature of android10. Every app in android gives feature of private folder for storage of required different number of media files. Now we will see media storage files can be accessed with the help of right permissions of android10 and different alternatives which can be used within scoped storage & how to use java.io.file api if the storage framework of android10 doesn’t fit within your needs.

 

Android App File Storage Architecture

In app storage architecture, it consists of dedicated number of directories either on the internal storage, primary external storage or secondary external storage of device & these directories are meant for to store media files which are not accessible to other apps of the device. All media content of these directories are erased once the app is successfully uninstalled. Below mention are the file storage methods or functions which return app storage directories:

  1. getFilesDir(): This method returns the app specific directory on the internal storage of device & this directory is deleted once the app is uninstalled. It returns the absolute path of the directory on file ecosystem where it actually created. The returned path may be change over time if calling app is shifted to some other storage device & there is no additional permissions need to call by app to read or write files under the returned paths.

 

  1. getExternalFilesDir(String Type): It returns the app specific directory on the primary external storage of your device & files under this also get deleted once the app is uninstalled. It returns the absolute path of the directory where application saves persistent files at its own. These files are not visible to users & serves as internal files to applications.

 

  1. getCacheDir(): This returns the app specific cache directory on the internal storage of device and files exists here get deleted if the memory of device is low or entirely deleted once the app is uninstalled.

 

  1. getExternalCacheDir(): This method or function returns the app specific cache directory on the primary external storage of the device.

 

  1. Shared Storage: These shared storage directories saves that files which are supposed to share with other applications of device consisting media files, documents & other files. Shared storage directories include download, images or pictures, movies and DCIM storage folders on any device.

Accessing Android Media Storage Files

Android media files include images, audios and videos can be accessed with the help of media store api’s but for this purpose your android application will have either one of two permissions which are read_external_storage and write_external_storage permissions and you must mention these permissions in the application’s manifest.xml file when your app starts at first time. Earlier before the launch of android10 “MediaStore.Audio.Media.DATA, MediaStore.Video.Media.DATA & MediaStore.Images.Media.DATA” storing variables were used for accessing media files with the help of java.io.file api but all as of now all these are outdated or deprecated after the release of android10 & forced adoption of scoped storage.

Conclusion:

In this blog post, we have understand the working of media files and scoped storage of android 10 which every android developer should start using it as it is going to be mandatory part of android application development.

rhombus-infotech-media-files-handling-android-development