This error appears in several other posts here, but I think that even though I've followed what is in them, I'm still not getting the proper result.
I'm defining the call to getUriFromFile like this in my java class:
File imagePath = new File(file.getAbsolutePath());
Uri contentUri = FileProvider.getUriForFile(Objects.requireNonNull(getContext().getApplicationContext()),
packageName+".fileprovider", imagePath);
The file is being saved in the folder: /data/data/{packageName}/images
I'm setting the provider like this in my AndroidManifest:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="{packageName}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
Note: {packageName} is literally the name of the package like com.package.packagename, for example.
And finally my provider_paths XML file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
What am I doing wrong?
/data/data/{packageName}/images
That path is unreachable for FileProvider.
Make it:
/data/data/{packageName}/files/images
It should be in getFilesDir() to begin with.
Related
I am trying to send a list of files using tcp socket but i get this file provider error.
please help if anyone can. Thanks
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains >/storage/708A-1A0F/- CHAIN YE DILLAN DA - MUHAMMAD UMAIR ZUBAIR QADRI - OFFICIAL HD VIDEO.mp4
at >androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:800)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:442)
File Path
Set<String> keys = BaseApplication.sendFileStates.keySet();
if (keys.size() > 0) {
for (String s : keys) {
File newFile = new File(s);
Uri contentUri = FileProvider.getUriForFile(this, "com.Jubilant.wifiproject", newFile);
uris.add(contentUri);
}
Log.d("contentUri", "onCreate: " + uris);
}
file_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="." />
<cache-path
name="external_cache"
path="."/>
<external-cache-path
name="external_cache_path"
path="."/>
<external-files-path
name="external_files_path"
path="."/>
<external-media-path
name="external_media_path"
path="."/>
<files-path
name="files_path"
path="."/>
</paths>
manifest.xml:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.Jubilant.wifiproject"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_path" />
</provider>
FileProvider standard can not serve files from a removable micro sd card.
Sometimes it can when you add
<root name="root" path="." />
to your file_paths.xml file.
In other cases write your own file provider extending ContentProvider.
So I am trying to create a pdf in my app and send that pdf as attachment to gmail app. All was good before android 11 scoped storage restrictions.
This are the files I am using :
provider_paths.xml :
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
AndroidManifest.xml :
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths" />
</provider>
This is the file that I am trying to add as attachment :
File filelocation = new File(context.getCacheDir() +"/", "Some#" + printBean.getId() + ".pdf");
Using the below to access Uri :
Uri path = FileProvider.getUriForFile(this, getPackageName()+".fileprovider",filelocation);
but when I am passing the Uri with the intent for opening gmail
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{new UserPreference(context).getUser().getEmail()});
if (path != null) emailIntent.putExtra(Intent.EXTRA_STREAM, path);
emailIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Mail PDF");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new UserPreference(context).getUser().getEmail());
emailIntent.putExtra(Intent.EXTRA_TEXT, printBean);
context.startActivity(Intent.createChooser(emailIntent, context.getString(R.string.send_email)));
it gives me error "couldn't attach file".
Need Help.
Edit : Getting this exception in log :
ComposeActivity:Error adding attachment
ggx: FileNotFoundException when openAssetFileDescriptor.
at ggy.a(PG:6)
at ggy.a(PG:45)
at dod.a(PG:146)
at dnk.run(PG:2)
at dod.a(PG:184)
at dod.a(PG:153)
at dod.a(PG:452)
at dkb.a(Unknown Source:3)
at aezj.a(Unknown Source:5)
at agzq.a(PG:2)
at agzs.run(PG:9)
at ahcg.run(Unknown Source:7)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at aexk.run(PG:2)
at adjn.run(Unknown Source:3)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Well FiltUtils might be returning null. You can get the file like
val file = File(context.getCacheDir(), FILE_NAME) and afterwards pass
FileProvider.getUriForFile(requireContext(),YOUR_PROVIDER_PACKAGE ,file)
File provider will give you the uri then you can pass that inside intent of sharing file
I'm having really hard time trying to open downloaded apk
I'm trying to use FileProvider (https://developer.android.com/reference/android/support/v4/content/FileProvider), I've followed this steps but I think I'm doing something wrong. I change this code in different ways many times, but unsuccessful.
Here's what I have:
File downloaded in:
app.setPathUpdate(Environment.getExternalStorageDirectory() +
File.separator + "trovata/update/" +
this.getApplicationInfo().packageName + File.separator)
The code above make this outcome: /storage/emulated/0/trovata/update/br.com.trovata.milano.elite/
Here's what I'm doing:
SincronizacaoActivity.java
File FileAppInst = new File(app.getPathUpdate() + "atualizador");
Uri apkUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".provider", FileAppInst);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<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/filepaths" />
</provider>
filepaths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="atualizador" path="/trovata/update/${applicationId}/"/>
</paths>
Error:
...
Caused by: java.lang.IllegalArgumentException: Failed to find configured
root that contains /storage/emulated/0/trovata/update/br.com.trovata.milano.elite/atualizador
...
Could you help me?
path="/trovata/update/${applicationId}/"
${applicationId} only turns into your application ID in AndroidManifest.xml. You cannot use manifest placeholders in resource files. So, replace ${applicationId} with your actual application ID.
In all the encounters with FileProvider it occurs that in most of the cases it only worked when the path was set to path=".".
Replace your filepaths.xml with this
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="atualizador" path="."/>
</paths>
And give it a try.
I'm trying to open a video file stored in my device using the default media player(VLC).
Code:
public void startPlay(View view) {
String name = "VideoName.mkv";
Uri path = FileProvider.getUriForFile(this, "com.packagName.AppName",
new File("storage/[sd card directory]/Videos/"+name));
Log.d("PATHVAR", path.toString());
Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("video/*");
startActivity(shareIntent);
}
On the other hand, when I use Environment.getExternalStorageDirectory().getAbsolutePath(), it doesn't throw any errors.
Manifest:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.packageName.AppName"
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 xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<cache-path name="cache" path="." />
<external-cache-path name="external_cache" path="." />
<files-path name="files" path="." />
</paths>
Logcat:
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/6262-6133/Videos/VideoName.mkv
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.packageName.AppName.MainActivity.startPlay(MainActivity.java:201)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:7339)
at android.widget.TextView.performClick(TextView.java:14222)
at android.view.View.performClickInternal(View.java:7305)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27787)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7091)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Any idea why this is happening? I've set up the necessary permissions and am able to create a text file in internal storage. I've searched around for a solution to this but most of them are using internal storage, not the sd card.
Thanks.
I'm creating an share image button, but for some reason I'm getting such a error code:
java.lang.IllegalArgumentException: Failed to find configured root that contains /document/image:74
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
at com.jawad.photoeditor.MainActivity$5.onClick(MainActivity.java:163)
my Manifest looks like:
<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/fileprovider" />
</provider>
and my fileprovider.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
And my specific code looks like :
final Button shareButton = findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File(imageUri.getPath());
Intent intent = new Intent(Intent.ACTION_SEND);
FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", file);
intent.setDataAndType(imageUri, "image/*");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(intent);
}
});
Tried this, but it didn't resolve the issue. Any help would be much appreciated, thanks in advance!
So i've just tried adding an root path in my xml file.
<root-path name="root" path="." />
and it now works as a charm. Issue solved