Android FileProvider cannot find file - android

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.

Related

Prompting user to install APK

I'm building my own update mechanism for my Android app (I'm not using Play Store). I can successfully download a new apk using DownloadManager. Once the download is finished, I would like to prompt the user to install the apk. I have tried the following approach:
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
String test = file.getAbsolutePath();
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("content://" + file.getAbsolutePath()),
"application/vnd.android.package-archive");
context.startActivity(promptInstall);
Unfortunately, this does not work at all. The file is there but there is no installation prompt. What is going wrong? Do I also have to adapt the second argument to setDataAndType? It should work on SDK 23 to 29.
Edit:
I have now tried the following approach:
File file = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS + "/app-debug.apk");
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(promptInstall);
In manifest I have now the following:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="ir.greencode"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/path" />
</provider>
And I have created the path.xml file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." /></paths>
But now I'm getting the error message:
java.lang.IllegalArgumentException: Couldn't find meta-data for
provider with authority com.testapp.provider
My downloaded APK is located in /storage/emulated/0/Android/data/com.testapp/files/Download/app-debug.apk. That means file is pointing to that directory. In the end, I would like to store the apk either on internal storage or SD storage (depending on where more space is available).
How can I resolve the problem?
Possible duplicate of Install Application programmatically on Android
I think this answer from that thread should work:
https://stackoverflow.com/a/54424223/9490453
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

FileProvider : Install apk programatically - Failed to find configured root that contains

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.

FileProvider with external folder

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?

Xamarin Android 7+ install APK programmatically

I am trying to install an .apk I have downloaded to the downloads folder in Android 7.
I have tried the way recommended in a number of StackOverflow posts and here https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en by using a FileProvider:
File file = new File(fileUri);
//using Android.Support.V4.Content;
var downloadUri = FileProvider.GetUriForFile(context,context.ApplicationContext.PackageName + ".com.package.name.provider", file);
Intent install = new Intent(Intent.ActionInstallPackage);
install.AddFlags(ActivityFlags.GrantReadUriPermission);
install.AddFlags(ActivityFlags.GrantWriteUriPermission);
install.AddFlags(ActivityFlags.GrantPersistableUriPermission);
install.SetDataAndType(downloadUri, "application/vnd.android.package-archive");
context.StartActivity(install);
AndroidManifest.xml
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<application android:label="Settings" android:icon="#drawable/Icon" android:theme="#style/myTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.com.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
</application>
provider_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
The "downloadUri" looks like: "content://com.package.name.com.package.name.provider/external_files/Download/Sensors%2520Multitool_1.3.0_apk-dl.com.apk"
The error when the installation window pops up is: "There was a problem parsing the package".
I have installed this package by clicking on it in the downloads folder and it installs fine, I have also tried other .apk's with the same issue.
File file = new File(fileUri);
if(Build.VERSION.SdkInt >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", toInstall);
Intent intentS = new Intent(Intent.ActionInstallPackage);
intentS.SetData(apkUri);
intentS.SetFlags(ActivityFlags.GrantReadUriPermission);
context.StartActivity(intentS);
} else {
Uri apkUri = Uri.FromFile(toInstall);
Intent intentS = new Intent(Intent.ActionView);
intentS.SetDataAndType(apkUri, "application/vnd.android.package-archive");
intentS.SetFlags(ActivityFlags.NewTask);
context.StartActivity(intentS);
}
It appears that the issue in parsing the package was due to the space in the package name "Sensors%2520Multitool_1.3.0_apk-dl.com.apk".
As soon as the space was removed the package installed correctly.

Android taking picture with FileProvider

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!

Categories

Resources