I've looked at several examples like File provider throws exception but I can't seem to get my file provider to work for everyone. I've tested it myself on Android 8 and it works fine, but for some users it throws IllegalArgumentException.
It looks like:
Manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.my.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
Paths file
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="cache_dir" path="/" />
</paths>
Sharing:
File path = context.getExternalCacheDir().getAbsolutePath();
path += filename;
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Shared file");
sendIntent.putExtra(Intent.EXTRA_STREAM, getUriForFile(context, "com.my.app.fileprovider", path));
sendIntent.setType("application/octet-stream");
context.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.strBackup)));
The exception:
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/3764-3933/Android/data/com.my.app/cache/backup.bin
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
...
What should the file provider paths file looks like to work for all users? And why does it work for most Android 8+ users, but not all? Should I use a path like . or ./ instead?
Related
I am using FileProvider to install apk from my app. Followed many stackoverflow questions but still facing this issue
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.tssi.myapptour/files/download/myapp_newSigned.apk
My provider in manifest
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.tssi.myapptour.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
my file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="download" path="download/" />
<!-- <external-path name="download" path="Android/data/com.tssi.myapptour/files/download/" />-->
</paths>
And my code for calling apk
String strApkToInstall = "myapp_newSigned.apk";
File fileApkToInstall = new File(getExternalFilesDir("download"), strApkToInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri fileUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider",
fileApkToInstall);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
Your file is in getExternalFilesDir(). That does not match files-path in your FileProvider metadata. Either:
Switch to getFilesDir() (a much better solution from a security standpoint), or
Switch to external-files-path
See the FileProvider documentation for more about the mapping between filesystem locations and metadata entries.
I'm trying to open a file (using the FileProvider) that is stored in a private folder
File appFiles = new File(externalPath.getAbsolutePath() +
"/Android/data/com.my.nice.domain/files/Container/");
Then inside the appFiles i'm creating subfolder and store files inside the sub folder
my files_path.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_path4"
path="Android/data/com.my.nice.domain/files/Container/"/>
</paths>
Relevant manifest xml code snippet
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.my.nice.domain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
and this is how I'm using the FileProvider to open the files (to view them with the default viewer)
File f = new File(url);
String AUTHORITY="com.my.nice.domain.fileprovider";
Uri uri = FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, f);
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
but for some reason its not working, and I'm getting error saying that file not found
example for error message that I'm getting
File not found (content://com.my.nice.domain.fileprovider/my_path4/576feb25/535d8216.xlsx)
Any ideas on what am I doing wrong?
I am relatively new to Android programming and followed several different tutorials.
The target was to copy a pdf-file from the assets folder to the external storage and then open an Intent when a button is pressed to open a PDF-Viewer. I tried to adjust my code to the latest Android version using FileProvider as described here: https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en
Opening the file on older Android versions worked so I know that the whole copying process is working, the thing is I just can't figure out where the error in my code is.
File file = new File(Environment.getExternalStorageDirectory() + "/" + "index.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
//old version
Uri fileURI = Uri.fromFile(file);
//new version
Uri fileUri = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider", file);
getApplicationContext().grantUriPermission(PACKAGE_NAME, fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(fileUri,"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
I set up my FileProvider as described in the above link.
<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>
using the following 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 would be really glad if someone could explain me my error.
From the comments: I had to add intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); also.
I'm taking a picture on Android Nougat with FileProvider, that's my code
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
file_paths.xml:
<paths>
<files-path name="img" path="images/" />
</paths>
Java:
String fileName = "cameraOutput" + System.currentTimeMillis() + ".jpg";
File imagePath = new File(getContext().getFilesDir(), "images");
File file = new File(imagePath, fileName);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final Uri outputUri = FileProvider.getUriForFile(activity, "com.mypackage.fileprovider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
getContext().grantUriPermission(
"com.google.android.GoogleCamera",
outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
);
cameraIntent.setFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
activity.startActivityForResult(cameraIntent, ACTIVITY_REQUEST_CODE);
Camera activity starts, take picture successfully, but the Activity Result is not OK and the only thing I see in error logs is
CAM_StateSavePic: exception while saving result to URI: Optional.of(content://com.mypackage.fileprovider/img/cameraOutput1469383289530.jpg)
FileNotFoundException: Is a directory
EDIT
missed File#createNewFile();
The main thing to get camera and gallery URI working is provider paths.
you need to create a provider_paths.xml to /res/xml/ dir
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
In your manifest file add this lines between
<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>
One more thing, I've found that we need to set vmPolicy in Application class like this
#Override
public void onCreate() {
super.onCreate();
//Allowing Strict mode policy for Nougat support
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
Double check your camera and external storage permission in manifest file and there you go with nougat URI.
For more details check this link : Android N FileUriExposedException
if androidx was used in your project, you need replace dot(.) to slash(/).
<!--别用. 不然在androidx上面有问题-->
<external-path name="sdcard" path="/" />
works for me great!
I have some files in custom folder in the internal storage. I need to open the files using external application like Adobe. I planned to open using FileProvider. But the third party application is stopped when trying to open the the document. I really don't know what mistake I did in the code.
package name = "com.example.doc"
activities are in package = "com.example.doc.activities"
manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.doc.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:readPermission="com.example.doc.fileprovider.READ">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepath" />
</provider>
res/xml/filepath.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="." />
</paths>
activity
//tempfilePath = /data/data/com.example.doc/files/RBC/sample.docx
File file = new File(tempfilePath);
Uri exportUri = FileProvider.getUriForFile (this, "com.example.doc.fileprovider", file);
Intent docViewIntent = new Intent();
docViewIntent.setAction(Intent.ACTION_VIEW);
docViewIntent.setDataAndType(exportUri , extensionType);
docViewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
docViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(docViewIntent);
Do we need to specify any permission in manifest?
Any help would be appreciated..
Thanks
Jomia