Scoped storage for Above Android 10 using Java & Upload to Server Using Multipart Form Retrofit

Bhupathi Turaga
4 min readFeb 10, 2021

A secure and advanced app storage system for Android.

Before Android 10, storage was divided into private storage and shared storage. Private storage’s(android/data/<package name>) access and contribution was limited to owner app alone. Apart from private storage, rest of the storage was called shared storage which is where all the media and non-media files were being stored. Any app with storage permission would be able to access this part of storage.

With Android 10, Google started to restructure the storage and change the way app accesses the storage so that all the mentioned problems can be eliminated. This storage division is called scoped storage.

What all permissions app need to access the files from scoped storage?

Earlier there was one permission to read all the files and another to write. Now with scoped storage, access is allotted based upon storage type and the ownership of the content.

  1. App will have unlimited access to their internal and external storage for both read & write operation.
  2. Media collection contributed by other apps can be accessed using ‘READ_STORAGE_PERMISSION’ permission. ‘WRITE_STORAGE_PERMISSION’ permission will be deprecated from next version and if used, will work same as ‘READ_STORAGE_PERMISSION’.
  3. 4. Non Media files contributed by other apps can be accessed using Storage access framework API. No explicit permission is needed. This does not mean that App can get access to all the directories ( Root, android/Data , Download directory etc.). Once user grants access to it, it will be complete access. (Read, Modify, Delete).

Now Coming To Main Part…

The Shortcut way to skip this..

Android 9 can still request the requestLegacyExternalStorage attribute and Access using Read and Write Permissions. But Android 10 or Above have to follow.

First of all Permissions..

Add Permissions In Manifest File

Next Use Dexter for Request Permissions..

Used Dexter for Request Multiple Permissions

Now Open Media Files or Documents Using Intent..

Next Activity Result Comes in…In Activity Result We take Result Intent Data from data.getData(). Initialize a Global Variable fileUri and Attach Result Intent data to it..

Attaching Result Data to fileUri

Now Lets Go to Upload File to Server..

Before Uploading File. In Scoped Storage We don't have direct access to files ,So We need to Get file from fileUri and Create a ParcelFileDescriptor and Open fileUri Using getContentResolver().openFileDescriptor() and attach to parcelFileDescriptor.

Now Create a New Input File Stream and Attach parcelFileDescriptor.getFileDescriptor().

Create a File and Make Global Variable with Path Cachedir and keep name of the original file.Now Create FileOutputStream and this create file.

Now Using IOUtils Copy inputstream Obtained from fileUri to Output Stream Which was in the Application CacheDir.Our AppDir can be Accessed According to Scoped Storage Rules 😜.

Last Part Upload this outputstream file Using Retrofit Multipart

Make an Interface File for Multipart File
Create File to Request Body…
Upload body to server using Retrofit..

Everything Done. Output file will be in your Cache.

Lastly, thank you for reading the article! Any questions and suggestions are most welcome. See you soon.

--

--