从内部存储中获取共享首选项 .xml 文件的 URI

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

我正在创建一个 ACTION_SEND Intent 来发送关于 device/application

当前状态的不同信息

我无法将共享首选项文件 URI 附加到该意图

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);

其中 fileUriList 是一个 ArrayList<Uri> 包含:

供应商如何在清单中注册

<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>

这就是我尝试获取共享首选项文件的 URI 的方式:

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

我收到以下错误消息:

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

如何从内部私有内存中正确获取共享首选项 .xml 文件?

FileProvider 不会从任意位置提供服务,并且 shared_prefs 不在其支持范围内。您需要创建自己的 ContentProvider 来提供该文件。

对于 Android 10- 将此添加到您的 privider_paths.xml 文件中:

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

否则将您的文件复制到 getFilesDir() 并使用以下方式从那里共享:

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