
📸 Android: Comparing FileProvider vs. MediaStore + Top Interview Questions
📸 Android: Comparing FileProvider vs. MediaStore + Top Interview Questions 관련


In the world of modern android development,Jetpack Compose has revolutionized UI building, making it more intuitive and efficient. But what if you need a simple function to download, save, and share images within your app? Whether you are building a social media app, a gallery viewer, or just n2222eed to hand images dynamically,this guide will walk you through the comparison betweenFileProvider
andMediaStore
as well as suggest some of interview questions. Now let’s go 🔥
- 📂 What is Android
FileProvider
? - 🔄
FileProvider
in a Multi-Module Android Project? - 📸 What is Android
Media Store
? - 🚨Common Error & Solution
- 📌 Questions & Answers
📱 Access app-specific files
In many cases, your app creates files that other apps don’t need to access, or shouldn’t access. The system provides the following locations for storing suchapp-specificfiles:
- **Internal storage directories:**These directories include both a dedicated location for storing persistent files, and another location for storing cache data. The system prevents other apps from accessing these locations, and on Android 10 (API level 29) and higher, these locations are encrypted. These characteristics make these locations a good place to store sensitive data that only your app itself can access.
- **External storage directories:**These directories include both a dedicated location for storing persistent files, and another location for storing cache data. Although it’s possible for another app to access these directories if that app has the proper permissions, the files stored in these directories are meant for use only by your app. If you specifically intend to create files that other apps should be able to access, your app should store these files in theshared storagepart of external storage instead.
Info
When the user uninstalls your app, the files saved in app-specific storage are removed. Because of this behavior, you shouldn’t use this storage to save anything that the user expects to persist independently of your app. For example, if your app allows users to capture photos, the user would expect that they can access those photos even after they uninstall your app. So you should instead use shared storage to save those types of files to the appropriatemedia collection.
Scoped Storage & Why It Matters in Android 10+ 🚀
InAndroid 10 (API 29)and above, Google introducedScoped Storageto improve app security and user privacy. This changerestrictshow apps can access files on external storage. andMediaStoreis the recommended way to interact with media files.
BeforeScoped Storage, apps could freely read/write toEnvironment.getExternalStorageDirectory()
, but thisposed security riskssince apps could access files from other apps.
Note
If your app requests a storage-related permission at runtime, the user-facing dialog indicates that your app is requesting broad access to external storage, even when scoped storage is enabled.
✅ What Scoped Storage Changes
- Apps can no longerfreely accessall files in external storage (
/sdcard/
). - Instead, apps can only accesstheir own app-specific directoriesinside:
Android/data/com.example.app/
Android/media/com.example.app/
- Direct
file://
URIs areblocked(causesFileUriExposedException
). - Apps mustuse
FileProvider.getUriForFile()
to share files.
- If an app wants to share a file (e.g., an image) with another app (WhatsApp, Gmail, etc.), it must use:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
- This grants temporary permission for the receiving app to read the file.
- Apps should use
MediaStore
APIsto read/writepublicfiles (e.g., Photos, Downloads).
Feature | Android 10+ (Scoped Storage) | Android 9- (FileProvider) |
---|---|---|
Where is image saved? | Pictures/ using MediaStore | Pictures/MyApp/ manually |
Requires WRITE_EXTERNAL_STORAGE ? | ❌ No | ✅ Yes (Android 9-) |
How to share? | Direct Uri from MediaStore | FileProvider URI |
Compatible with all apps? | ✅ Yes (Gallery, WhatsApp, etc.) | ✅ Yes (With FileProvider) |
What is AndroidFileProvider
?
FileProvider
is a special subclass ofContentProvider
in Android that allows you to securely share files between different applications without exposing the internal storage file paths. It provides a way to generatecontent://
URIs instead offile://
URIs, which prevents issues with theScoped StorageandFile URI Exposure Exceptionon modern Android versions.
- ✅ Securely share files between apps.
- ✅ Prevent
FileUriExposedException
on Android 7.0+. - ✅ Works withcamera intents, sharing files via email, social media, …
- ✅ Supports multiple modules in a project.
📌 Basic Implementation ofFileProvider
1. Specify theFileProvider
inAndroidManifest.xml
Defining aFileProvider
for your app requires an entry in your manifest. This entry specifies the authority to use in generating content URIs, as well as the name of an XML file that specifies the directories your app can share.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
2. Specify sharable directoriesfilepaths.xml
inres/xml/
Once you have added theFileProvider
to your app manifest, you need to specify the directories that contain the files you want to share. To specify the directories, start by creating the filefilepaths.xml
in theres/xml/
subdirectory of your project. In this file, specify the directories by adding an XML element for each directory. The following snippet shows you an example of the contents ofres/xml/
filepaths.xml
. The snippet also demonstrates how to share a subdirectory of thefiles/
directory in your internal storage area:
<?xml version="1.0" encoding="utf-8"?>
<!-- Allow access to app-specific external storage -->
<paths>
<!-- 📂/storage/emulated/0/Android/data/{package_name}/**/images/-->
<external-files-path name="images" path="images/" />
<files-path name="myimages" path="images/" />
</paths>
In this example:
external-files-path
: Refers to the app’s external storageexternal-cache-path
: Refers to the app’s external cache directory<files-path>
tag shares directories within thefiles/
directory of your app’s internal storage. Thepath
attribute shares theimages/
subdirectory offiles/
. Thename
attribute tells theFileProvider
to add the path segmentmyimages
to content URIs for files in thefiles/images/
subdirectory.

context.cacheDir
Note
The XML file is the only way you can specify the directories you want to share; you can’t programmatically add a directory.
3. Define theBuildConfig
- BuildConfig
buildConfigField("String", "FILE_PROVIDER", "\"provider\"")
FileProvider
import androidx.core.content.FileProvider as AndroidFileProvider
interface FileProvider {
@Throws(Exception::class)
fun getUri(context: Context, file: File): Uri? {
val authority = authority(context)
return AndroidFileProvider.getUriForFile(context, authority, file)
}
companion object : FileProvider {
val authority = { context: Context ->
"${context.applicationContext.packageName}.${BuildConfig.FILE_PROVIDER}"
}
}
}
🆚FileProvider
in a Multi-Module Android Project?
By default,FileProvider
is tied to anauthority
(e.g.,com.example.app.fileprovider
), which is unique per module. In amulti-module project, each modulecannot define its own FileProvider
with the sameauthority
. Instead, follow these steps:
DeclareFileProvider
in the App Module
- Allow feature modules to access
FileProvider
- Pass the
Context
of the main application to the modules. - Use
FileProvider.getUriForFile(...)
in the modules with theapp’s authority(com.example.app.fileprovider
).
Use Approach 1 (App Module) if:
- Your project is small or medium-sized.
- You don’t plan to reuse
FileProvider
in multiple apps. - Simplicity is more important than modularity.
Create a Shared Module for FileProvider
- Create a new module (e.g.,
foundation-io
). - Define the
FileProvider
infoundation-io
and expose a method: - All modules will use this method to get the URI.
Use Approach 2 (Shared Module) if:
- Your project is large, with multiple feature modules.
- You want todecouple
FileProvider
from the main app. - You might reuse the shared module in other projects.
- Your team followsclean architecture and modularizationbest practices.
Note
If you’re working on alarge-scale projectwith multiple modules (e.g., a super app, SDK-based architecture),Approach 2 (Shared Module) is the best choicebecause it promotesscalability, reusability, and maintainability. However, if youjust need quick file sharing in a single app,Approach 1 (App Module) is simpler and works fine.
📸 What is AndroidMedia Store
?
The contract between the media provider and applications. Contains definitions for the supported URIs and columns. The media provider provides an indexed collection of common media types, such asAudio,Video, andImages, from any attached storage devices. Each collection is organized based on the primary MIME type of the underlying content; for example,image/*
content is indexed underImages. TheFilescollection provides a broad view across all collections, and does not filter by MIME type.
1. Query Media Files (Images)
public void getAllImages(Context context) {
Uri collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME
};
Cursor cursor = context.getContentResolver().query(
collection,
projection,
null, null,
MediaStore.Images.Media.DATE_ADDED + " DESC"
);
if (cursor != null) {
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
Uri imageUri = Uri.withAppendedPath(collection, String.valueOf(id));
Log.d("MediaStore", "Image: " + name + ", URI: " + imageUri);
}
cursor.close();
}
}
2. Insert a New Image
public Uri saveImageToGallery(Context context, Bitmap bitmap, String filename) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/MyApp");
Uri imageUri = context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try {
OutputStream outputStream = context.getContentResolver().openOutputStream(imageUri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
return imageUri;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

Note
- The
RELATIVE_PATH
specifies where the image is stored (Pictures/MyApp
). - NoWRITE_EXTERNAL_STORAGEpermission needed fromAndroid 10+.
3. Delete a Media File
public boolean deleteImage(Context context, Uri uri) {
int deleted = context.getContentResolver().delete(uri, null, null);
return deleted > 0;
}
🔹 Permissions Needed
- Android 10+: No storage permission required, as long as using
MediaStore
. - Android 9 and below: Requires
WRITE_EXTERNAL_STORAGE
permission.
4. Update Media File Metadata
public void updateMediaDetails(Context context, Uri uri, String newTitle) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, newTitle);
context.getContentResolver().update(uri, values, null, null);
}
The system automatically scans an external storage volume and adds media files to the following well-defined collections:
- **Images,**including photographs and screenshots, which are stored in the
DCIM/
andPictures/
directories. The system adds these files to theMediaStore.Images
table. - **Videos,**which are stored in the
DCIM/
,Movies/
, andPictures/
directories. The system adds these files to theMediaStore.Video
table. - **Audio files,**which are stored in the
Alarms/
,Audiobooks/
,Music/
,Notifications/
,Podcasts/
, andRingtones/
directories. Additionally, the system recognizes audio playlists that are in theMusic/
orMovies/
directories as well as voice recordings that are in theRecordings/
directory. The system adds these files to theMediaStore.Audio
table.TheRecordings/
directory isn’t available on Android 11 (API level 30) and lower. - **Downloaded files,**which are stored in the
Download/
directory. On devices that run Android 10 (API level 29) and higher, these files are stored in theMediaStore.Downloads
table.This table isn’t available on Android 9 (API level 28) and lower.
🚨Common Error & Solution
1. SecurityException
: Permission Denied
java.lang.SecurityException: Permission Denial: writing Uri content://media/external/images/media from pid=12345, uid=10085 requires android.permission.WRITE_EXTERNAL_STORAGE
- Your app is targetingAndroid 10+and trying to write to shared storagewithout using MediaStore.
- You did not requestWRITE_EXTERNAL_STORAGE(Android 9 and below).
- ✅ UseMediaStoreAPI for Android 10+ instead of direct file paths.
- ✅No permission requiredfor public media directories on Android 11+.
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, "my_image.jpg");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
2. File Not Found Exception
java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
- Thefile path is incorrector does not exist.
- You aretrying to access a file before it has been saved.
- ✅Verify the file existsbefore accessing it.
- ✅ Ensure you havewrite permissionsif using direct storage access.
3. Image Not Appearing in Gallery
Media file is saved, butnot indexed by MediaScanner
.
✅Manually scan the fileafter saving.
MediaScannerConnection.scanFile(context,
new String[]{file.getAbsolutePath()},
new String[]{"image/jpeg"},
(path, uri) -> Log.d("MediaScanner", "File Scanned: " + path));
4. FileUriExposedException
(Android 7+)
Direct file paths (file://
) are not allowedfor sharing files across apps in Android 7+.
android.os.FileUriExposedException: file:///storage/emulated/0/my_file.pdf exposed beyond app through ClipData.Item.getUri()
✅ UseFileProviderto generate acontent URIinstead.
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", new File(filePath));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
5. android:authorities
Not Matching
java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.example.wrong.fileprovider
The**authority name in AndroidManifest.xml
**does not match the one used inFileProvider.getUriForFile()
.
✅ Ensure theandroid:authorities
inAndroidManifest.xml
matches exactly.
6. Permission Denied When Opening a Shared File
java.lang.SecurityException: Permission Denial: reading Uri content://com.example.myapp.fileprovider/my_files/test.pdf
The receiving appdoes not have permissionto read the file.
✅Grant temporary read permissionswhen sharing a file.
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
7. Failed to find configured root that contains …

/storage/emulated/0/Pictures/..
The error**“Failed to find configured root that contains…”usually occurs when trying to share a filewithout proper FileProvider configuration**. InAndroid 7.0 (API 24) and above, directfile://
URIs cannot be shared between apps due to security restrictions. Instead, you need to use acontent://
URI viaFileProvider
.
- Inside the
<application>
tag:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- Allow access to app-specific external storage -->
<external-files-path name="pictures" path="Pictures/" />
</paths>
- Code
// App-specific external storage
val picturesDir = File(context.getExternalFilesDir(null), "Pictures")
picturesDir.mkdirs() // Ensure the directory exists
📌 Questions & Answers
1. What is MediaStore
in Android?
MediaStore
is an API that provides structured access tomedia files(images, videos, audio) stored onexternal storage. It allows querying, inserting, and updating media fileswithout requiring direct file path access.
2. How much space does your data require?
Internal storage has limited space for app-specific data. Use other types of storage if you need to save a substantial amount of data.
3. How reliable does data access need to be?
If your app’s basic functionality requires certain data, such as when your app is starting up, place the data within internal storage directory or a database. App-specific files that are stored in external storage aren’t always accessible because some devices allow users to remove a physical device that corresponds to external storage.
4. What are the advantages of using MediaStore
over File API?
✅No storage permission required(Android 10+).
✅Works with scoped storage(File API does not).
✅Structured accessto media files.
✅Auto indexingby the system.
5. What is FileProvider
in Android?
FileProvider
is acontent provider(ContentProvider
) that allowssecure file sharingbetween apps usingcontent://
URIs instead offile://
paths.
6. Should the data be private to your app?
When storing sensitive data — data that shouldn’t be accessible from any other app — use internal storage, preferences, or a database. Internal storage has the added benefit of the data being hidden from users.
7. What is Scoped Storage in Android?
Scoped Storage (introduced in Android 10)restrictsdirect access to shared storage and requires apps to useMediaStore
orFileProvider
.
8. How do you access files in Scoped Storage?
- **For media files:**Use
MediaStore
API - **For app-specific files:**Use
getExternalFilesDir()
- **For file sharing:**Use
FileProvider
To give users more control over their files and limit file clutter, Android 10 introduced a new storage paradigm for apps calledscoped storage. Scoped storage changes the way apps store and access files on a device’s external storage. To help you migrate your app to support scoped storage, follow the best practices for common storage use cases that are outlined in this guide. The use cases are organized into two categories:handling media filesandhandling non-media files.
🎯 Conclusion
By leveragingJetpack Compose,FileProvider
, and Kotlin, we can easilydownload, store, and share imagesin an Android app. This approach ensures smooth performance, proper file handling, and seamless sharing across different applications. Whether you are working on an image-heavy app or simply adding a sharing feature, mastering these techniques will enhance your development workflow 🚀💡
🔹 MediaStore
(Scoped Storage API)
MediaStore
is part of theScoped Storagemodel introduced inAndroid 10 (API 29).- It provides access topublic media files(e.g., images, videos, and audio) stored in shared storage, without needing
READ_EXTERNAL_STORAGE
orWRITE_EXTERNAL_STORAGE
permissions (from Android 11 onwards).
- Saving media files (images, videos, audio) inpublic directories(e.g.,
Pictures/
,Movies/
). - Querying and managing media content using the
ContentResolver
API. - Ensuring compliance withScoped Storage, as direct file path access is restricted.
- ✅ No need for storage permissions (from Android 11+).
- ✅ Works withScoped Storage, avoiding direct file access.
- ✅ Provides acontent URI, making it safer for media handling.
- ✅ Compatible with
MediaScanner
for indexing media files.
- ❌ Cannot storenon-media files(e.g., PDFs, ZIPs).
- ❌ Requiresquerying APIs(instead of direct file access)
- ❌ Files arenot private— they’re accessible to all apps with read access to shared storage.
🔹FileProvider (For Secure File Sharing)
FileProvider
is a specialContentProvider
that allows an app toshare private filessecurely with other apps using acontent URI, rather than exposing raw file paths.
- Sharing**files (PDFs, images, videos, etc.)**with other apps (e.g., email, social media, or messaging apps).
- Providing a secure way to exposeapp-private storage files(inside
data/data/<package>/files
). - Grantingtemporary read/write accessto specific files.
- ✅Secure file sharing(avoids exposing raw file paths).
- ✅ Supportsany file type, not just media files.
- ✅ Providestemporary access permissionsusing
FLAG_GRANT_READ_URI_PERMISSION
. - ✅ Works withintent-based sharing(e.g.,
Intent.ACTION_SEND
).
- ❌ Requires defining aFileProvider in AndroidManifest.xml.
- ❌ NeedsXML configurationfor file paths.
- ❌More complex setupcompared to direct file sharing.
Info
This article is previously published on proandroiddev
