android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

๐Ÿ“ฑ Android Nougat Crash: FileUriExposedException Explained! ๐Ÿ˜ฑ

Are you encountering a dreading ๐Ÿ˜– crash while trying to open a file in your Android app? Does it work smoothly on older versions but tumbles ๐Ÿคน on Android Nougat? Are you wondering if it's a permission problem? ๐Ÿค” Don't worry! You've come to the right place! ๐ŸŽ‰

What's Happening? ๐Ÿคทโ€โ™‚๏ธ

Let's break it down! ๐Ÿ•ต๏ธโ€โ™‚๏ธ

When you try to open a file from the SD card using the Intent.ACTION_VIEW action, you might encounter the android.os.FileUriExposedException. This exception occurs because starting from Android Nougat (API level 24), file:// URIs are not allowed anymore. They are considered insecure as they expose sensitive data to other apps. ๐Ÿšซ

The Sample Code ๐Ÿ“

Here's a snippet of the sample code where the crash occurs:

File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line

How to Fix It? ๐Ÿ› ๏ธ

To fix the FileUriExposedException, we need to use content:// URIs instead of file:// URIs. However, there's a catch! Your app needs to open files in root directories. ๐Ÿ˜ฎ Don't worry; I've got a solution for you! ๐Ÿ™Œ

  1. First, we need to make use of the File Provider ๐Ÿ—‚๏ธ class provided by Android.

    • Add the following snippet to your AndroidManifest.xml file within the <application> tags:

      <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.myfileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
    • In the above snippet, replace ${applicationId} with your actual application id.

  2. Create a new XML file in the res/xml directory named file_paths.xml (create the xml directory if it doesn't exist) and add the following code:

    <paths xmlns:android="http://schemas.android.com/apk/res/android"> <root-path name="root" path="/" /> </paths>
    • This configuration allows your app to access files in root directories.

  3. Now, let's modify the previous code snippet to use content:// URIs through the File Provider:

    File file = new File("/storage/emulated/0/test.txt"); Uri contentUri = FileProvider.getUriForFile(this, "${applicationId}.myfileprovider", file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(contentUri, "text/*"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent);
    • Replace ${applicationId} with your actual application id, just like in the manifest.

  4. We also need to update the file's FileProvider path in the file_paths.xml file. Add the following code inside the <paths> tag:

    <root-path name="external_files" path="."/>
    • With this, the File Provider will have access to the root directory of your files.

Wrap Up and Engage! ๐ŸŽ

Hurray! ๐ŸŽ‰ You've just solved the FileUriExposedException problem on Android Nougat and still manage to access files in root directories! ๐ŸŒŸ

Remember, targeting newer Android versions requires adapting to their security measures. But don't worry, we've got your back! ๐Ÿ”’

If you found this blog post helpful, comment below and let me know how it worked for you. ๐Ÿ—ฃ๏ธ Share this article with your fellow developers who might be facing a similar issue. Together, we can conquer any Android obstacle! ๐Ÿš€

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

๐Ÿ”ฅ ๐Ÿ’ป ๐Ÿ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! ๐Ÿš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings ๐Ÿ’ฅโœ‚๏ธ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide ๐Ÿš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? ๐Ÿค” Well, my