简体   繁体   English

缺少 android.support.FILE_PROVIDER_PATHS 元数据

[英]Missing android.support.FILE_PROVIDER_PATHS meta-data

I am trying to open the pdf file which I downloaded & saved in external storage.我正在尝试打开我下载并保存在外部存储中的 pdf 文件。 But When I open my app its gets crashed and showing following error;但是当我打开我的应用程序时,它会崩溃并显示以下错误;

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) 

Following is my async task where I download that pdf file & trying to open it.以下是我下载该 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);
        }

    }

Following is AndroidManifest.xml以下是 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>

And provider_paths.xml having following code;并且 provider_paths.xml 具有以下代码;

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path path="Android/data/" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>

I am trying to run above code in API 19 & API 25, but in both devices its gets crash.我试图在 API 19 和 API 25 中运行上面的代码,但在这两种设备中它都会崩溃。 I am not able to find out where I am going wrong.我无法找出我哪里出错了。

I have referred this question & also this link too but not able to find a solution to my issue.我也提到了这个问题这个链接,但无法找到解决我的问题的方法。

In your AndroidManifest file, you have a slight typo in the metadata:在您的 AndroidManifest 文件中,您在元数据中有一个轻微的拼写错误:

<meta-data
    android:name="android.support.FILE_PROVIDER_PATHS" <-- HERE!!!
    android:resource="@xml/provider_paths" />

You need to use FILE_PROVIDER_PATHS instead of FILE_PROVIDE_PATHS.您需要使用 FILE_PROVIDER_PATHS 而不是 FILE_PROVIDE_PATHS。

Please do invalidate caches and restart function in android studio, most probably that will do.请在android studio中使缓存无效并重新启动功能,很可能会这样做。

After that rebuild and run your app.之后重建并运行您的应用程序。 In my case it worked perfectly fine.就我而言,它工作得很好。

Seems like something is set wrong, The compiler doesn't find android.support.FILE_PROVIDER_PATHS , thus, it throws this error, check if everything is set correctly in your gradle file, if you change the package of the android.support this exception might arise.好像设置错误,编译器没有找到 android.support.FILE_PROVIDER_PATHS ,因此,它抛出这个错误,检查你的gradle文件中的所有设置是否正确,如果你更改了android.support的包,这个异常可能出现。

After fixing, do a clean, rebuild, or clean and invalidate cache in Android studio.修复后,在 Android Studio 中执行清理、重建或清理并使缓存失效。

this is often caused by a typo, where you might use android:value instead of android:resource in the meta-data tag specifying the file_paths.xml file.这通常是由打字错误引起的,您可能会在指定file_paths.xml文件的元数据标记中使用android:value而不是android:resource For example you might have:例如,您可能有:

android:value="@xml/file_paths"

instead of而不是

android:resource="@xml/file_paths"

Provider in AndroidManifest.xml, if you using AndroidX library. 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>

There was no problem when using before (same test machine), but suddenly this problem appeared, I do not know what the reason is.I looked up some references, changed the suffix, and the problem was solved之前用的时候没有问题(同试机),突然出现这个问题,不知道是什么原因,查了一些参考资料,改了后缀,问题解决了

Previous code以前的代码

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

The modified code修改后的代码

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

I recently faced the same issue.我最近遇到了同样的问题。 and no other solution worked for me.没有其他解决方案对我有用。 I closed the project from Android Studio and then manually deleted the .gradle and .idea files and it worked like a charm :) .我从 Android Studio 关闭了该项目,然后手动删除了 .gradle 和 .idea 文件,它就像一个魅力:)。 And earlier i had an issue with the authorities - point it to the fileprovider you use with your applicationId .早些时候我遇到了当局的问题 - 将其指向您与 applicationId 一起使用的 fileprovider 。 Hope this helps someone.希望这可以帮助某人。

Sometimes everything with your code is just fine, but you switch branches and the old build intermediates are reused and this happens.有时你的代码一切都很好,但是你切换了分支,旧的构建中间体被重用,这就会发生。 For me, just removing the build/intermediates dir, and then building and running the app again worked.对我来说,只需删除build/intermediates目录,然后再次构建和运行应用程序即可。 This is almost equivalent (but faster) to cleaning and rebuilding your project.这几乎等同于(但更快)清理和重建您的项目。

Old post but I had the same issue.旧帖子,但我遇到了同样的问题。

If you have a line that looks something like this:如果你有一条看起来像这样的行:

FileProvider.getUriForFile(activity,"${BuildConfig.APPLICATION_ID}.fileProvider", tmpFile)

Just make sure that the fileProvider string matches exactly what is declared in your manifest:只需确保fileProvider字符串与清单中声明的​​内容完全匹配:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileProvider" //<--MATCH THIS!!
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

And note that is is case sensitive .请注意,这是区分大小写的

That solved it for me, anyway.无论如何,这为我解决了它。

Android:转换值 + 资源=@xml/file_paths"

我遇到了这个问题,只需删除所有 .gradle 和 build 文件夹,然后重建应用程序对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM