androiduriandroid-fileprovider

Android Fileprovider unable to get uri


I use Fileprovider to get Uri of an image I saved manually in a folder "F1" in Device Storage.When I tried to get the Uri,i get NullPointerException. The Exception is at where I try to access the Uri. I have declared provider in Manifest and declared the paths in xml file.Have I set the paths wrong in xml?

Code:

 folder=new File(Environment.getExternalStorageDirectory()+"/F1");
folder.mkdirs();
 File file1=new File(folder+"/37deb43.jpg");
            Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);

Manifest.xml:

<provider
            android:authorities="${applicationId}"
            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/share_file_paths"
                >
            </meta-data>
        </provider>

@xml/share_file_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
    name="external_files"
    path="F1">
</external-files-path>
</paths>

Exception:

 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:604)
        at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:578)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:416)
        at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.fetchuri(JavaTechnicalActivity.java:179)
        at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity.access$200(JavaTechnicalActivity.java:30)
        at com.globemaster.com.architecturecomponentstest.mvvm.java_technical_programs.JavaTechnicalActivity$4.onClick(JavaTechnicalActivity.java:130)
        at android.view.View.performClick(View.java:5716)
        at android.widget.TextView.performClick(TextView.java:10926)
        at android.view.View$PerformClick.run(View.java:22596)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Solution

  • I finally got it.The problem was in the second argument in getUriForFile().

    Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID+".provider",file1);
    

    I appended "provider" to "BuildConfig.APPLICATION_ID" below.It was not needed.The below code works fine now.But thank you all for your effort in trying to help me ..

    Uri uri= FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID,file1);