-3

This is my code block:

notificationIntent.setDataAndType(FileProvider.getUriForFile(
            context,
            "TrySomething",new File(context.getFilesDir().getAbsolutePath() + "/update_file")
    ),ANDROID_PACKAGE);

And this is my XML file:

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

But i get this error:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.trysomething/files/update_file

How can I fix it?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103

2 Answers2

0

First Create Xml File inside src->xml folder Like for example named file_paths_public.xml

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

then Add this Code in to your manifest file

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="<your app package>.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths_public" />
    </provider>

now create one function that will support both api 26 and below device that returns some valid uri

 public static Uri getUriFromFile(Context theCtx, File theSrcPath) {
        Uri requirdUri;

        // Above Compile SDKversion: 25 -- Uri.fromFile Not working
        // So we have to use Provider

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            requirdUri = FileProvider.getUriForFile(theCtx,
                    theCtx.getApplicationContext().getPackageName() + ".provider",
                    theSrcPath);
        } else {
            requirdUri = Uri.fromFile(theSrcPath);
        }

        return requirdUri;
    }

now use this uri Whenever you want like

 File FilePath = Environment.getExternalStorageDirectory().toString()/update_file/<here your file name >
   Uri shareImageUri = getUriFromFile(this, <your full file Path Add hear>)
Learning Always
  • 1,563
  • 4
  • 29
  • 49
-1
  1. Check if you have this in your manifest

    <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/provider_paths"/>
    </provider>
    
  2. provider_paths:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
    <external-path name="directory_name" path="."/>
    </paths>
    
  3. Get file URI as

    Uri fileUri = FileProvider.getUriForFile(context, getApplicationContext().getPackageName() + ".provider", fileName(FILE))
    
piet.t
  • 11,718
  • 21
  • 43
  • 52
Pradeep Kumar
  • 102
  • 1
  • 7