当我尝试使用ShareCompat共享my_file.html时,我遇到了一个奇怪的问题。
作为最佳实践,我创建了自己的FileProvider但是我收到了错误java.lang.IllegalArgumentException: Failed to find configured root that contains /forms/Proof_of_Life_Form.html
我认为我已经正确设置了所有内容。
我的XML我为此创建了一个提供程序。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.douglas.sample"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
这是我在filepaths
res/xml/filepaths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path path="/forms" name="forms" />
</paths>
以及负责调用共享内容的方法。
public void shareContent() {
File file = new File("/forms/", fileName + ".html");
Uri uri = FileProvider.getUriForFile(getContext(), "com.douglas.sample", file);
Intent shareIntent = ShareCompat.IntentBuilder.from(getActivity())
.setType(getActivity().getContentResolver().getType(uri))
.setStream(uri)
.getIntent();
//Provide read access
shareIntent.setData(uri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_form)));
}
最后我的例外:
java.lang.IllegalArgumentException: Failed to find configured root that contains /forms/Proof_of_Life_Form.html
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:719)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:404)
at com.douglas.sample.TabbedFormsFragment$2.run(TabbedFormsFragment.java:254)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
任何想法?谢谢:D
答案 0 :(得分:1)
您的代码似乎有一些小错误。
尝试以下方法:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
然后在您的provider_paths.xml中尝试:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<!-- We would like to give access to the External Storage of the app.
external_files will expose the path app to storage/emulated/0/ and the
"." is to actually point the root folder. The final path of the Uri will start
from path above and whatever folder we defined. For application upgrades
will be external_files/Android/data/package name/files/updates/name of update apk.-->
<external-path
name="external_files"
path="."/>
</paths>
然后,如果您想使用它,请尝试:
String path = Uri.parse(your_file_uri).getPath();
FileProvider.getUriForFile(application, PROVIDER, new File(path)),my_file_type);
其中my_file_type =例如.apk文件的“application / vnd.android.package-archive”
希望它有所帮助!!!