Upload files with uri on Android

Tram Ho

You probably already know that the words on the new android operating systems help to better protect application data and users on external memory. Direct operations via filePath will no longer be available to android developers.

And Google has also created a flag called requestLegacyExternalStorage on Android 10 to give developers more time to update their apps to be compatible with this new update. However, this flag will be useless on Android 11 (More details can be found at this full HD link ). Failure to get the file path field anymore will cause relatively difficult to handle files on Android such as reading files, uploading files, …

In this article I will suggest you methods to be able to upload files via uri.

Suppose I have api to create users with the necessary Body including “name”, “date_of_birth” with an image file that is the user’s avatar.

Choose a photo from the gallery

Create request body from Uri

The function takes mediaType of file in class Utils

Call api add User

Share the news now

Source : Viblo