FileProvider NPE null object reference - android

Hi I'm currently experiencing error on starting an intent due to FileProvider causing NPE. I followed the steps provided here but I still got the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
I am not sure as well if the provider_paths.xml is being used here. Here's my code so far:
Manifest:
<application>... <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider></application>
res/xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
code:
Uri uri=FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID+".provider", file);
the itself seems to be the cause of error here.

BuildConfig.APPLICATION_ID should return your packagename, however, in my case it returned "android.support.v4". I consider using getPackagename() and it will work just fine!
So just change
FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider", file);
to:
FileProvider.getUriForFile(this,
getPackagename() + ".provider", file);

Refer to this link for your answer.
enter link description here
First, this:
android:authorities="${applicationId}.fileprovider"
you are using this:
FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider", file);
chabge this :
FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".fileprovider", file);
Since I don't know which of those is what you really want, I cannot suggest a fix.

I am using this :
Manifest :
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
XML file :
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="media_folder" path="." />
</paths>
In Java :
FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider", file);

Related

How to fix "java.lang.IllegalArgumentException: Failed to find configured root"?

My FileProvider:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.bignerdranch.android.criminalintent.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/files"/>
</provider>
files.xml:
<paths>
<files-path name="my_images" path="images/"/>
</paths>
Trying to get uri:
File testFile = new File(getContext().getFilesDir(), "my_images");
File testFile2 = new File(testFile, "cool.jpg");
Uri uri = FileProvider.getUriForFile(getContext(),
"com.bignerdranch.android.criminalintent.fileprovider",
testFile2);
The last method crashes the app:
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.bignerdranch.android.criminalintent/files/my_images/cool.jpg
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:800)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:442)
at com.bignerdranch.android.criminalintent.CrimeFragment.onViewCreated(CrimeFragment.java:214)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
...
I did it according to the official site but for some reason it doesn't work.

'Couldn't find meta-data for provider with authority android.support.compat.provider but i'm using androidx.core.content.Fileprovider

When I try to use
var apkURI = FileProvider.GetUriForFile(AndroidApp.Context, BuildConfig.ApplicationId + ".provider", file);
I get
Java.Lang.IllegalArgumentException: 'Couldn't find meta-data for provider with authority android.support.compat.provider'
android manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
provider_paths.xml
<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
I have no idea why i'm getting this error and all sources i try to find to help yields in no information to solve it, any help would be greatly appreciated

Android OREO - java.lang.SecurityException: Failed to find provider for user 0; expected to find a valid ContentProvider for this authority

I followed the below tutorial and modified code
https://medium.com/#egemenhamutcu/fixing-securityexception-requiring-a-valid-contentprovider-on-android-8-1110d840522
I defined the provider in the manifest file.
<provider
android:name="com.test.PhotoProvider"
android:authorities="${applicationId}.PhotoProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
provider paths has the below line
<external-path name="/storage/emulated/0" path="/" />
I’m loading the uri using below code
Uri decodedImgUri = PhotoProvider.getPhotoUri(new File(imgPath));
ContentResolver cr = activity.getContentResolver();
cr.notifyChange(decodedImgUri, null);
ImageLoader.getInstance().displayImage(decodedImgUri.toString(), holder.image);
public static Uri getPhotoUri(File file) {
Uri outputUri = Uri.fromFile(file);
Uri.Builder builder = new Uri.Builder()
.authority(CONTENT_PROVIDER_AUTHORITY)
.scheme("file")
.path(outputUri.getPath())
.query(outputUri.getQuery())
.fragment(outputUri.getFragment());
return builder.build();
}
This is how I defined my authority
public static final String CONTENT_PROVIDER_AUTHORITY = "com.test.PhotoProvider";
But when I ‘m trying to load the uri into an image view, I keep getting the below error.
java.io.FileNotFoundException: com.test.PhotoProvider/data/user/0/com.test/cache/cropped953508219.jpg: open failed: ENOENT (No such file or directory)
This is happening even on < 8.0 version.
Try this one ...
In manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.xyz.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
and In xml/provider_path file
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/com.your.package/" name="files_root"/>
<external-path name="external_files" path="."/>
</paths>
Hope it will help...!

java.lang.IllegalArgumentException: Failed to find configured root that contains

This is my code block:
notificationIntent.setDataAndType(FileProvider.getUriForFile(
context,
"TrySomething",new File(context.getFilesDir().getAbsolutePath() + "/update_file")
),ANDROID_PACKAGE);
And this is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="myupdater" path="files/update_file"/>
</paths>
But i get this error:
java.lang.IllegalArgumentException: Failed to find configured root
that contains /data/data/com.example.trysomething/files/update_file
How can I fix it?
First Create Xml File inside src->xml folder Like
for example named file_paths_public.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
<root-path
name="external_files"
path="/storage/"/>
</paths>
then Add this Code in to your manifest file
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="<your app package>.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths_public" />
</provider>
now create one function that will support both api 26 and below device
that returns some valid uri
public static Uri getUriFromFile(Context theCtx, File theSrcPath) {
Uri requirdUri;
// Above Compile SDKversion: 25 -- Uri.fromFile Not working
// So we have to use Provider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
requirdUri = FileProvider.getUriForFile(theCtx,
theCtx.getApplicationContext().getPackageName() + ".provider",
theSrcPath);
} else {
requirdUri = Uri.fromFile(theSrcPath);
}
return requirdUri;
}
now use this uri Whenever you want
like
File FilePath = Environment.getExternalStorageDirectory().toString()/update_file/<here your file name >
Uri shareImageUri = getUriFromFile(this, <your full file Path Add hear>)
Check if you have this in your manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
provider_paths:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="directory_name" path="."/>
</paths>
Get file URI as
Uri fileUri = FileProvider.getUriForFile(context, getApplicationContext().getPackageName() + ".provider", fileName(FILE))

IllegalArgumentException: Failed to find configured root that contains filePath

Trying to open an image in the default gallery app. I can't resolve this issue following the steps:
My file path is /data/data/{applicationName}/cache/file.jpg
Intent:
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(filePath);
intent.setDataAndType(FileProvider.getUriForFile(this, getApplicationContext()
.getPackageName() + ".provider", file),"image/*");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
</paths>
I've tried changing the path and name in provider_paths but no luck.
Replace:
<external-path
name="external_files"
path="." />
with:
<cache-path name="external_files" />
as your path seems to be in getCacheDir().

Categories

Resources