Reputation: 1517
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)
Upvotes: 0
Views: 2835
Reputation: 2028
my file provider path is this :
<paths><root-path name="sdcard1" path="." /></paths>
and this code is how i get uri
val uri = FileProvider.getUriForFile(
getApplicationContext(),
getApplicationContext().packageName,
File(
Environment.getExternalStorageDirectory(),
it.path?.split(":")?.lastOrNull()
)
)
Upvotes: 0
Reputation: 1517
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);
Upvotes: 1
Reputation: 15679
I guess you have a copy-paste error in your first snippet! Try this:
File 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);
Log.d("URI", "Uri is: " + uri)
Upvotes: 0