Pinki
Pinki

Reputation: 21929

how to attach internal memory file in android

I have created one pdf dynamically and saved into application internal memory under one folder. Now i want to share that pdf over mail or other available sharing options.

I am facing

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)

Below one is my code

Manifest File

<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: it is under 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

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 );

Any one suggest me where am doing wrong Thanks in Advance!!!

Upvotes: 0

Views: 1094

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007276

android:authorities="mypkgName.fileprovider"

does not match:

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

As a result, FileProvder cannot find the ContentProvider to read its XML metadata. You need to pass in the proper authority string to getUriForFile().

Since you set android:authorities to "mypkgName.fileprovider", use:

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

(getApplicationContext() is pointless here)

Upvotes: 1

Related Questions