0

I Have Been Trying To Open A File using intent and FileProvider but app crashes when i try to open the file..Here's what i am doing...please help me out..

@Override
public void onClick(View view){
final Uri uri = FileProvider.getUriForFile(mCtx, String.valueOf(R.string.file_provider_authority), new File(mydata.get(getAdapterPosition()).getPath()));
    mCtx.grantUriPermission(mCtx.getPackageName() , uri , Intent.FLAG_GRANT_READ_URI_PERMISSION);

    try {
        final Intent intent = new Intent(Intent.ACTION_VIEW)
                .setDataAndType(uri, "audio/wav")
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        Intent choose = Intent.createChooser(intent, "See..");
        mCtx.startActivity(choose);
        Toast.makeText(mCtx, "Clicked " + mydata.get(getAdapterPosition()).getPath(), Toast.LENGTH_SHORT).show();
    }

    catch (
            ActivityNotFoundException e
            ){

    }


   }

}

I got following exception --------- beginning of crash

2018-12-18 09:13:40.173 25265-25265/my.uapp.com E/AndroidRuntime: FATAL EXCEPTION: main Process: my.uapp.com, PID: 25265 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:605) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:579) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417) at my.uapp.com.MyAdapter$ViewHolder.onClick(MyAdapter.java:73) at android.view.View.performClick(View.java:6320) at android.view.View$PerformClick.run(View.java:25087) at android.os.Handler.handleCallback(Handler.java:869) at android.os.Handler.dispatchMessage(Handler.java:101) at android.os.Looper.loop(Looper.java:206) at android.app.ActivityThread.main(ActivityThread.java:6749) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:845)

Amanth Rai
  • 191
  • 2
  • 12
  • Look at [the stack trace](https://stackoverflow.com/questions/23353173) to determine the cause of the crash. – Mike M. Dec 18 '18 at 03:30
  • Thanks for suggesting...here's what i got in stack – Amanth Rai Dec 18 '18 at 03:49
  • That Exception suggests that the authority you're passing in the `FileProvider.getUriForFile()` call does not match what you've specified on the `` element in your manifest. Those must match exactly. – Mike M. Dec 18 '18 at 03:55
  • Thanks for your reply... so the provider have to be same as in manifest declaration... ☺ – Amanth Rai Dec 18 '18 at 03:58
  • Yes, the `String` you're passing as the second argument in `FileProvider.getUriForFile()` must exactly match what you have for the `authorities` attribute on the `` in your manifest. Check the value you have there against your `file_provider_authority` resource string. – Mike M. Dec 18 '18 at 04:01
  • 1
    Thank you.... Mike M. I edited my code... And here's it's working now... THANK YOU So much you are awesome... ☺️ – Amanth Rai Dec 18 '18 at 04:02
  • 1
    Cheers... Thank you so much.. – Amanth Rai Dec 18 '18 at 04:05

0 Answers0