Search code examples
androidandroid-contentprovider

Get URI of the shared preferences .xml file from the internal storage


I'm creating an ACTION_SEND Intent that sends different information regarding the current state of the device/application

I'm having trouble attaching the shared preferences file URI to that intent

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setData(Uri.parse("mailto:"));
intent.setType("application/*");
intent.putExtra(Intent.EXTRA_EMAIL  , new String[] {"support@example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
intent.putExtra(Intent.EXTRA_TEXT, "Email body - Debug info");
// THE IMPORTANT PART
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,fileUriList);

where fileUriList is an ArrayList<Uri> containing:

  • the SQLite database file which I successfully got from the ContentProvider
  • the shared preferences file URI - which I am not able to get

How the provider is registered in the manifest

<provider
    android:name="._android._providers.MyFileProvider"
    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>

@xml/provider_paths:

<paths>
    <cache-path name="cache_internal_storage" path="." />
    <external-path name="external_files" path="."/>
</paths>

This is how I'm trying to get the URI to the shared preferences file:

String spFilePath = getApplicationInfo().dataDir + "/shared_prefs/main_sp.xml";
File sharedPrefsMain = new File(spFilePath);
Uri spUri = MyFileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", sharedPrefsMain);

I'm getting the following error message:

Failed to find configured root that contains /data/data/ro.example.example/shared_prefs/main_sp.xml

How can I properly get the shared preferences .xml file from the internal private memory?


Solution

  • For Android 10- add this to your privider_paths.xml file:

     <root-path name="root" path="." />
    

    Otherwise copy your file to getFilesDir() and share from there using:

     <files-path name="private" path="."/>