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.
Related
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"/>
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 am trying to install apk after it has been downloaded by the download manager. I am using a broadcast to get the message after download completes. This is my code below:-
FileProvider: Installing APK. There was an error parsing the package.
val file = File(getExternalFilesDir("Download"), "/playlist/update.apk")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val apkUri = FileProvider.getUriForFile(this#MainActivity, BuildConfig.APPLICATION_ID + ".provider", file)
val intent = Intent(Intent.ACTION_INSTALL_PACKAGE)
intent.data = apkUri
intent.flags= Intent.FLAG_GRANT_READ_URI_PERMISSION
this#MainActivity.startActivity(intent)
} else {
val apkUri = Uri.fromFile(file)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
this#MainActivity.startActivity(intent)
}
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/filepaths" />
</provider>
filespath.xml :-
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="Download" path="Download/" />
</paths>
I am downloading the apk in "Downloads/playlist/" dir. I saw this same problem - here. However I have can't seem to get it working from that solution. I also checked the apk by installing it myself and it gets installed without any errors. Please help.
I want to show a notification and when user clicked install an APK file.
I know how to use the fileProvider itself but I have problem with service context.
Here is my code
manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.company.app"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
filePaths:
<paths>
<external-path
name="externalFiles"
path="." />
</paths>
service Code:
resultIntent = new Intent(Intent.ACTION_VIEW);
Uri uri;
File file = FileHelper.getApkFile(item.getLink());
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(
NotificationService.this,
NotificationService.this.getApplicationContext().getPackageName(),
file);
NotificationService.this.setResult(Activity.RESULT_OK,
resultIntent);
} else {
uri = Uri.fromFile(file);
}
resultIntent.setDataAndType(uri, "application/vnd.android.package-archive");
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
as you know setResult() is not available in service.
How can I grant permission from service context?
thank you every one.
I found the problem
I had to add this permission to my manifest to install apk file.
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
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.