exposed beyond app through ClipData Item getUri

0 votes

I have been trying to fix a problem after the new feature was included in the Android file system but I get this ERROR:

android.os.FileUriExposedException: file:///storage/emulated/0/MyApp/Camera_20180105_172234.jpg exposed beyond app through ClipData.Item.getUri()


Could I please get help in getting past this ERROR ?

private Uri getTempUri() { 
      // Create an image file name 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); 
      String dt = sdf.format(new Date()); 
      imageFile = null; 
        imageFile = new File(Environment.getExternalStorageDirectory() 
                      + "/MyApp/", "Camera_" + dt + ".jpg"); 
AppLog.Log( 
                TAG, 
                "New Camera Image Path:- " 
                                    + Environment.getExternalStorageDirectory() 
                                    + "/MyApp/" + "Camera_" + dt + ".jpg"); 
File file = new File(Environment.getExternalStorageDirectory() + "/MyApp"); 
if (!file.exists()) { 
    file.mkdir(); 
} 
imagePath = Environment.getExternalStorageDirectory() + "/MyApp/" 
                + "Camera_" + dt + ".jpg"; 
imageUri = Uri.fromFile(imageFile); 
return imageUri; }
Feb 16, 2022 in Java by Soham
• 9,700 points
5,044 views

1 answer to this question.

0 votes

To solve this ERROR you need to set your sdk 24 and up, if you need to get the Uri of a file outside your app storage you have this error. However if you want to follow the google guideline without having to change the API policy of your app you have to use a FileProvider. At first to get the URI of a file you need to use FileProvider.getUriForFile() method:

Uri imageUri = FileProvider.getUriForFile( 
                      MainActivity.this, 
                      "com.example.homefolder.example.provider", //(use your app signature + ".provider" ) imageFile);


Then you need to configure who is your provider in your android manifest :
 

<application> 

        <provider 
              android:name="android.support.v4.content.FileProvider"  android:authorities="com.example.homefolder.example.provider" android:exported="false"   android:grantUriPermissions="true"> 
<!-- ressource file to create --> 
<meta-data 
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"> 
        </meta-data>
      </provider>
</application>

(In "authorities" use the same value than the second argument of the getUriForFile() method (app signature + ".provider"))

And finally you need to create the resources file: "file_paths". This file need to be created under the res/xml directory:


<?xml version="1.0" encoding="utf-8"?> 
<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
            <external-path name="external_files" path="." /> 
</paths>

answered Feb 16, 2022 by Aditya
• 7,680 points

Related Questions In Java

0 votes
0 answers

Ho do I Iterate through a HashMap which contains duplicate values

What is the fastest and the best ...READ MORE

Apr 16, 2018 in Java by Daisy
• 8,120 points
672 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
4,844 views
0 votes
1 answer

To use JavaMail API for sending mails in Android without using the default/built-in App

ADD 3 jars found in the following ...READ MORE

answered Jun 6, 2018 in Java by sridhar
• 160 points
1,257 views
0 votes
1 answer
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
1,505 views
0 votes
1 answer

How can we get the current location in Android?

First you need to define a LocationListener to handle ...READ MORE

answered Sep 25, 2018 in Java by Parth
• 4,630 points
749 views
0 votes
1 answer

Can't find class CognitoUserPoolsSignInProvider: Issue with Sign In integration

CognitoUserPoolsSignInProvider is ditributed as part of aws-android-sdk-auth-userpools library. Please import ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 58,090 points
679 views
0 votes
1 answer

How to Read/Write String from a File in Android

Writing a File in android: private void writeToFile(String ...READ MORE

answered Oct 3, 2018 in Java by sharth
• 3,370 points
6,975 views
0 votes
1 answer

Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio

In Android Studio 3.1, you can see ...READ MORE

answered Feb 17, 2022 in Java by Aditya
• 7,680 points
12,603 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP