添加我的 FileProvider 的问题

Issue with adding my FileProvider

如果我尝试像这样添加我的 FileProvider:

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

我遇到了以下问题:

Caused by: java.lang.RuntimeException: Manifest merger failed with multiple errors, see logs
    at com.android.builder.core.AndroidBuilder.mergeManifestsForApplication(AndroidBuilder.java:524)
    at com.android.build.gradle.tasks.MergeManifests.doFullTaskAction(MergeManifests.java:143)
    at com.android.build.gradle.internal.tasks.IncrementalTask.taskAction(IncrementalTask.java:106)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:50)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
    at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.run(ExecuteActionsTaskExecuter.java:131)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:317)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:309)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:185)
    at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:97)
    at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:120)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:99)

如果我删除 "authorities" 行,我会得到:

 Manifest merger failed : Attribute meta-data#android.support.FILE_PROVIDER_PATHS@resource value=(@xml/provider_paths) from AndroidManifest.xml:374:17-55
is also present at [io.smooch:ui:5.12.5] AndroidManifest.xml:50:17-51 value=(@xml/file_paths).
Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml:372:13-374:58 to override.

我还添加了 'tools:replace="android:resource"' 但它又回到了旧错误。我该怎么做才能正确合并它们?

编辑:

这是我的 providers_path.xml 文件:

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

这是我需要获取的文件:Environment.getExternalStorageDirectory() + "/Passenger/passenger_log.txt";

我需要在我的提供商路径中添加什么?试图理解外部路径的逻辑。

此刻我明白了:

06-11 11:30:51.483: E/AndroidRuntime(16656): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:604)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:578)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:416)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at nl.hgrams.passenger.fragments.PSSettingsFragment.onResponse(PSSettingsFragment.java:311)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at nl.hgrams.passenger.utils.Utils.onAnimationEnd(Utils.java:552)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.view.animation.Animation.run(Animation.java:382)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.os.Handler.handleCallback(Handler.java:808)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.os.Handler.dispatchMessage(Handler.java:101)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.os.Looper.loop(Looper.java:166)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at android.app.ActivityThread.main(ActivityThread.java:7425)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at java.lang.reflect.Method.invoke(Native Method)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
06-11 11:30:51.483: E/AndroidRuntime(16656):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

此代码导致崩溃,在 FileProvider 行:

  File file = new File(Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
                            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                            emailIntent.setType("plain/text");
                            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"support@psngr.co"});
                            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Psngr Android Logs");
                            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Thanks for helping us improve Psngr!\n Please describe the issue you found");
                            emailIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(getActivity(), Constants.PROVIDER_AUTHORITY, file));
                            startActivity(Intent.createChooser(emailIntent, "Send Feedback..."));
                            PSApplicationClass.getInstance().showSavingPopup = getString(R.string.email_sent);

在你的代码中 这条线

 android:authorities="${applicationId}.myfileprovider" 

应替换为

 android:authorities="${applicationId}.provider"

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