👨🏼‍💻 Intermediate: Get Images From Storage and with Location Data

Mehmet Yozgatlı
Huawei Developers
Published in
3 min readApr 11, 2022

--

Locations of Photos in Map

Introduction

Hello everyone. In this article, I will tell you how to access all the images in the storage and also how to get the location information of these images, if any.

You can find detailed information on this subject from the Android official document below:

https://developer.android.com/training/data-storage/shared/media

Implementation

  • Get All Photos From Storage

First of all, since we want to access the images from the storage, we need to get permission from the user.

You need to add the following permission to your Android Manifest file.

<uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” />

Then you need to get approval from the user for this permission. You can check Android official document below:

https://developer.android.com/training/permissions/requesting

Finally, the part you need to pay attention to is the Build Version. You can check Android official document below for the necessary configurations.

https://developer.android.com/training/data-storage/use-cases#opt-out-in-production-app

If your app targets Android 10 (API level 29) or lower, you can temporarily opt out of scoped storage in your production app. If you target Android 10, however, you need to set the value of requestLegacyExternalStorage to true in your app's manifest file:

After completing the permission and configuration processes, you can start writing the codes to get the images from the storage. You can check Android official document below:

https://developer.android.com/training/data-storage/shared/media#query-collection

The code I customized is as follows:

Get All Images From Storage

You now have the “path” of the images. You can show images to the user using RecyclerView and Glide.

  • Get Location Information From Images

In order to get the location information of the images in the Storage, you need first make the following configurations.

  1. Request the ACCESS_MEDIA_LOCATION permission in your app's manifest.
  2. From your MediaStore object, get the exact bytes of the photograph by calling setRequireOriginal() and pass in the URI of the photograph. You can examine this code block in the “getAllImagesFromSdCard” function I shared above.

Then you can get location information from images using the ExifInterface library :

You can create a helper class as follows to convert GPS values ​​from ExifInterface to values ​​that can be used on the map.

You can also find detailed information on this subject from the document below.

https://developer.android.com/training/data-storage/shared/media#location-media-captured

Output

Images:

All Images From Storage

Locations:

Conclusion

With the data we have obtained, you can develop an application that will attract the attention of the user and be useful. As you have noticed, permission and configuration processes are important for this use case.

I hope it was useful. See you in other articles :)

References

https://developer.android.com/guide

--

--