如何在Android中附加内部存储器文件



我已经动态创建了一个PDF,并将其保存到一个文件夹下的应用程序内存中。现在,我想通过邮件或其他可用共享选项共享该PDF。

我面对

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
                                                                                at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:560)
                                                                                at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
                                                                                at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)

下面是我的代码

清单文件

<provider
            android:authorities="mypkgName.fileprovider"
            android:name="android.support.v4.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

file_paths.xml:它在res/xml/file_paths

    <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="media"/>
    <files-path path="app_PDF_Export/" name="Report.pdf" />
    <files-path path="/" name="allfiles" />
</paths>

java文件

File newFile = new File(getActivity().getApplicationInfo().dataDir + File.separator +   Constants.folderName, getActivity().getResources().getString(R.string.pdf_name) + ".pdf");
//
  Uri sharedFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(), getActivity().getPackageName(), newFile);
            PackageManager packageManager = getActivity().getPackageManager();
            List<ResolveInfo> list = packageManager.queryIntentActivities(emailIntent, PackageManager.MATCH_DEFAULT_ONLY);
            if (list.size() < 1) {
                return;
            }
            String packageName = list.get(0).activityInfo.packageName;
            getActivity(). grantUriPermission(packageName, sharedFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            emailIntent.putExtra(Intent.EXTRA_STREAM, sharedFileUri );

任何人都建议我在哪里做错了预先感谢!

android:authorities="mypkgName.fileprovider"

不匹配:

FileProvider.getUriForFile(getActivity().getApplicationContext(), getActivity().getPackageName(), newFile);

结果,FileProvder找不到ContentProvider读取其XML元数据。您需要将适当的权威字符串传递给getUriForFile()

由于将android:authorities设置为"mypkgName.fileprovider",请使用:

FileProvider.getUriForFile(getActivity(), "mypkgName.fileprovider", newFile);

getApplicationContext()在这里毫无意义)

相关内容

  • 没有找到相关文章

最新更新