缺少android.support.FILE_PROVIDER_PATHS元数据

问题描述 投票:6回答:5

我正在尝试打开我下载并保存在外部存储中的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.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);
        }

    }

以下是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>

和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>

我试图在API 19和API 25中运行上面的代码,但在两个设备中它都会崩溃。我无法找出出错的地方。

我也提到了this questionthis link,但无法找到我的问题的解决方案。

android android-file android-fileprovider
5个回答
6
投票

在AndroidManifest文件中,元数据中有一个小错字:

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

FILE_PROVIDER_PATHS而不是FILE_PROVIDE_PATHS。


3
投票

请在android studio中使缓存和重启函数无效,最有可能的事情就是这样。

之后重建并运行您的应用程序。在我的情况下,它工作得很好。


1
投票

似乎设置错误,编译器找不到android.support.FILE_PROVIDER_PATHS,因此,它会抛出此错误,检查gradle文件中是否所有内容都设置正确,如果更改android.support的包,则此异常可能出现。

修复后,在Android Studio中执行干净,重建或清理并使缓存无效。


0
投票

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


-1
投票

这通常是由一个拼写错误引起的,你可以在指定file_paths.xml文件的元数据标签中使用android:value而不是android:resource。例如,你可能有:

android:value="@xml/file_paths"

代替

android:resource="@xml/file_paths"
© www.soinside.com 2019 - 2024. All rights reserved.