Xamarin Android install APK programmatically StartActivity doesnt do anything - android

Hello i'm trying to install an apk programmatically.
AndroidManifest.xml:
<application android:label="ABM.Ablesegeraet.Android">
<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">
</meta-data>
</provider>
</application>
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>
other code:
var context = Android.App.Application.Context;
Java.IO.File file = new Java.IO.File(GetExternalFilesDir("").AbsolutePath + $"/com.companyname.abm.ablesegeraet.apk");
var downloadUri = Xamarin.Essentials.FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".fileprovider", file);
Intent install = new Intent(Intent.ActionView);
install.AddFlags(ActivityFlags.NewTask);
install.AddFlags(ActivityFlags.GrantReadUriPermission);
install.AddFlags(ActivityFlags.GrantWriteUriPermission);
install.AddFlags(ActivityFlags.GrantPersistableUriPermission);
install.SetDataAndType(downloadUri, "application/vnd.android.package-archive");
context.StartActivity(install);
I don't get any errors but when context.StartActivity(install) is executed nothing happens.
What's the problem here?
//edit: this does not work in debug... In Release the activity does start but for some reasen it fails during the installation... It just says "App has not been installed" But why?
//edit2: to solve the "App has not been installed" error I had to delete the app and install it with a APK and not from visual studio. Also the update apk needs to have a higher build version than the installed one.

Related

Failed to find provider info for cn.teddymobile.free.anteater.den.provider

i have a question about this log, what does it mean ? i don't understand, but my bar chart has appeared. My data has not appeared. log :
E/ActivityThread: Failed to find provider info for cn.teddymobile.free.anteater.den.provider
Android Manifest :
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.cn.teddymobile.free.anteater.den.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"
tools:replace="android:resource" />
xml - > path :
<?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 resolved this issue by clearing the data on the application.
Just go to Settings -> App Management -> (name of your application) -> Storage Usage -> Clear Data.
just clear the app data of your app you are working on and you should be good to go..
I had this problem when trying to share a pdf file in Android 12 (it worked in Android 10), and solved it by adding FLAG_GRANT_READ_URI_PERMISSION flag in the opening intent. Like:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

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 throws exception for some Android 8 users

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?

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.

External application is stopped when using FileProvider for opening file from internal storage

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

Categories

Resources