本文介绍了java.lang.IllegalArgumentException:无法找到包含以下内容的已配置根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码块:
notificationIntent.setDataAndType(FileProvider.getUriForFile(
context,
"TrySomething",new File(context.getFilesDir().getAbsolutePath() + "/update_file")
),ANDROID_PACKAGE);
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="myupdater" path="files/update_file"/>
</paths>
但是我得到这个错误:
我该如何解决?
推荐答案
首先在src-> xml文件夹中创建Xml文件,例如例如名为file_paths_public.xml
First Create Xml File inside src->xml folder Likefor example named file_paths_public.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>
然后将此代码添加到清单文件中
then Add this Code in to your manifest file
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="<your app package>.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths_public" />
</provider>
现在创建一个将支持api 26及以下设备的函数返回一些有效的uri
now create one function that will support both api 26 and below devicethat returns some valid uri
public static Uri getUriFromFile(Context theCtx, File theSrcPath) {
Uri requirdUri;
// Above Compile SDKversion: 25 -- Uri.fromFile Not working
// So we have to use Provider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
requirdUri = FileProvider.getUriForFile(theCtx,
theCtx.getApplicationContext().getPackageName() + ".provider",
theSrcPath);
} else {
requirdUri = Uri.fromFile(theSrcPath);
}
return requirdUri;
}
现在只要您想使用此uri像
now use this uri Whenever you wantlike
File FilePath = Environment.getExternalStorageDirectory().toString()/update_file/<here your file name >
Uri shareImageUri = getUriFromFile(this, <your full file Path Add hear>)
这篇关于java.lang.IllegalArgumentException:无法找到包含以下内容的已配置根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!