【问题标题】:Get URI of the shared preferences .xml file from the internal storage从内部存储中获取共享首选项 .xml 文件的 URI
【发布时间】:2020-12-01 13:07:12
【问题描述】:

我正在创建一个ACTION_SENDIntent,它发送有关设备/应用程序当前状态的不同信息

我无法将共享首选项文件 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);

其中fileUriListArrayList<Uri>,其中包含:

  • 我从ContentProvider成功得到的SQLite数据库文件
  • 共享首选项文件 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);

我收到以下错误消息:

未能找到配置的根目录包含 /data/data/ro.example.example/shared_prefs/main_sp.xml

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

【问题讨论】:

    标签: android android-contentprovider


    【解决方案1】:

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

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

    否则,将您的文件复制到 getFilesDir() 并从那里共享:

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

    【讨论】:

      【解决方案2】:

      FileProvider 不会从任意位置提供服务,shared_prefs 超出了它所支持的范围。您需要创建自己的 ContentProvider 来提供该文件。

      【讨论】:

        猜你喜欢
        • 2011-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-12
        相关资源
        最近更新 更多