缺少android.support.FILE\u PROVIDER\u路径元数据
我正在尝试打开我下载并保存在外部存储器中的pdf文件。但当我打开我的应用程序时,它会崩溃并显示以下错误缺少android.support.FILE\u PROVIDER\u路径元数据,android,android-file,android-fileprovider,Android,Android File,Android Fileprovider,我正在尝试打开我下载并保存在外部存储器中的pdf文件。但当我打开我的应用程序时,它会崩溃并显示以下错误 08-31 00:58:31.304 1807-1807/? E/AndroidRuntime: FATAL EXCEPTION: main Process: medimanage.corporate.mobile, PID: 1807 java.lang.RuntimeException: Unable to get provider android.support.v4.conte
08-31 00:58:31.304 1807-1807/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: medimanage.corporate.mobile, PID: 1807
java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data
at android.app.ActivityThread.installProvider(ActivityThread.java:5856)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5445)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5384)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:586)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:557)
at android.support.v4.content.FileProvider.attachInfo(FileProvider.java:375)
at android.app.ActivityThread.installProvider(ActivityThread.java:5853)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5445)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5384)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
下面是我的异步任务,我下载了那个pdf文件并试图打开它
package myPackageName.common.postData;
public class DownloadFileFromURL extends AsyncTask<String, String, String> {
private ProgressDialog Dialog;
private Context mContext;
private String folder_main; //= "TermCondition";
private String fileName;//= "termCondition.pdf";
String urlString;
public DownloadFileFromURL(Context mContext, String folderName) {
this.mContext = mContext;
this.folder_main = folderName;
Dialog = new ProgressDialog(this.mContext);
}
@Override
protected String doInBackground(String... f_url) {
try {
urlString = f_url[0];
URL url = new URL(f_url[0]);
File folder = new File(Environment.getExternalStorageDirectory(), this.folder_main);
if (folder.exists()) {
folder.mkdirs();
}
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + "/" + "default.pdf");
int totalSize = urlConnection.getContentLength();
byte[] buffer = new byte[AccessibilityNodeInfoCompat.ACTION_DISMISS];
while (true) {
int bufferLength = inputStream.read(buffer);
if (bufferLength <= 0) {
break;
}
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
FileLog.e(mContext.getClass().getName(), e);
} catch (MalformedURLException e2) {
FileLog.e(mContext.getClass().getName(), e2);
} catch (IOException e3) {
FileLog.e(mContext.getClass().getName(), e3);
}
return null;
}
public void showPdf() {
try {
File file = new File(Environment.getExternalStorageDirectory() + "/" + "default.pdf");
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mContext, "medimanage.corporate.mobile.fileprovider", file);
intent.setDataAndType(contentUri, "application/pdf");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
}
mContext.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
Toast.makeText(mContext, "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
FileLog.e(mContext.getClass().getName(), e);
}
}
@Override
protected void onPreExecute() {
Dialog.setMessage("Downloading...");
Dialog.setCancelable(false);
Dialog.setCanceledOnTouchOutside(false);
Dialog.show();
}
@Override
protected void onPostExecute(String s) {
this.Dialog.dismiss();
showPdf();
super.onPostExecute(s);
}
}
包myPackageName.common.postData;
公共类DownloadFileFromURL扩展异步任务{
私人对话;
私有上下文;
私有字符串文件夹_main;//=“TermCondition”;
私有字符串文件名;//=“termCondition.pdf”;
字符串URL字符串;
公共下载FileFromURL(上下文mContext,字符串folderName){
this.mContext=mContext;
this.folder_main=folderName;
Dialog=新建ProgressDialog(this.mContext);
}
@凌驾
受保护的字符串doInBackground(字符串…f_url){
试一试{
urlString=f_url[0];
URL=新URL(f_URL[0]);
File folder=新文件(Environment.getExternalStorageDirectory(),this.folder\u main);
if(folder.exists()){
folder.mkdirs();
}
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.connect();
InputStream InputStream=urlConnection.getInputStream();
FileOutputStream FileOutputStream=新的FileOutputStream(Environment.getExternalStorageDirectory()+“/”+“default.pdf”);
int totalSize=urlConnection.getContentLength();
byte[]buffer=新字节[AccessibilityNodeInfoCompat.ACTION_disease];
while(true){
int bufferLength=inputStream.read(缓冲区);
if(bufferLength=Build.VERSION\u code.N){
intent.setFlags(intent.FLAG\u GRANT\u READ\u URI\u PERMISSION);
Uri contentUri=FileProvider.getUriForFile(mContext,“medimanage.corporate.mobile.FileProvider”,文件);
setDataAndType(contentUri,“application/pdf”);
}否则{
intent.setDataAndType(Uri.fromFile(文件),“application/pdf”);
}
mContext.startActivity(意图);
}捕获(ActivityNotFoundException anfe){
Toast.makeText(mContext,“找不到打开此附件的活动。”,Toast.LENGTH_LONG.show();
}
}捕获(例外e){
e(mContext.getClass().getName(),e);
}
}
@凌驾
受保护的void onPreExecute(){
setMessage(“下载…”);
对话框。可设置可取消(false);
对话框。setCanceledOnTouchOutside(false);
Dialog.show();
}
@凌驾
受保护的void onPostExecute(字符串s){
this.Dialog.disclose();
showPdf();
super.onPostExecute(s);
}
}
以下是AndroidManifest.xml
<application
...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="myPackageName.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDE_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
&但也无法找到我的问题的解决方案。似乎有些设置错误,编译器找不到android.support.FILE\u PROVIDER\u路径,因此,它会抛出此错误,请检查gradle文件中的所有设置是否正确,如果更改android.support的包,则可能会出现此异常
修复后,在Android studio中执行清理、重建或清理并使缓存无效。在您的AndroidManifest文件中,您的元数据有一点输入错误:
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS" <-- HERE!!!
android:resource="@xml/provider_paths" />
请在android studio中执行使缓存无效并重新启动功能的操作,很可能这样就可以了
然后重建并运行你的应用程序。在我的例子中,它工作得非常好。这通常是由于键入错误造成的,您可以在元数据标记中使用android:value而不是android:resource来指定文件路径.xml文件。例如,您可能有:
android:value="@xml/file_paths"
而不是
android:resource="@xml/file_paths"
Android:Convert value+resource=@xml/file\u路径“我最近也遇到了同样的问题。没有其他解决办法对我有效。我从Android Studio关闭了这个项目,然后手动删除了.gradle和.idea文件,它就像一个符咒:)。早些时候,我与当局有一个问题——将它指向您在applicationId中使用的文件提供程序。希望这对其他人有所帮助。我遇到了这个问题,只要删除所有.gradle和build文件夹,然后在AndroidManifest.xml中重新构建应用程序,如果您使用的是AndroidX库的话
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.yourdomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
以前使用(同一台测试机)时没有问题,但突然出现了这个问题,我不知道原因是什么。我查了一些参考资料,更改了后缀,问题就解决了
先前代码
android:authorities="${applicationId}.provider"
修改后的代码
android:authorities="${applicationId}.fileprovider"
有时,代码中的一切都很好,但是您切换了分支,旧的构建中间产物被重用,这就发生了。对我来说,只需删除build/intermediates
dir,然后再次构建并运行应用程序就行了。这几乎等同于(但更快)清理和重建您的项目。检查此项、此项和此@NileshRathod根据您引用的链接,我将替换provider_paths.xml文件中的以下行<代码>
但它仍然崩溃。它是否有效@priyanka kamtheno,它不适用于提供更多细节。比如什么问题?你的方法如何比公认的答案更好或不同等等。回答问题时,你可能应该更准确,解释问题的根源。你的意思是FILE\u-PROVIDE\u-path
是正确的方法吗?我不这么认为。
android:authorities="${applicationId}.fileprovider"