I've Looked through all other example questions and their answers to this problem I can find to no success, I believe the issue is that the MediaProvier/Intent call cannot access the Download folder, in the device log there are MediaProvider errors stating the file path cannot be found and an EACCES permission error.
I have permissions to READ/WRITE external storage and REQUEST_INSTALL_PACKAGES persmissions
The device emulator shows 'There was a problem parsing the package' What am I doing wrong and why can I not open/install the APK file? Any help would be appreciated.
In my opinion if I was to copy the apk to a path within the internal directory rather than external then this would work, however I also cannot copy the apk to any Android/data/ location using adb due to permissions issue.
Java.IO.File path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
Java.IO.File file = new Java.IO.File(path, "test2.apk");
// Get URI and MIME type of file
Android.Net.Uri uri = FileProvider.GetUriForFile(context, AppInfo.PackageName + ".fileprovider", file);
String mime = context.ContentResolver.GetType(uri);
// Open file with user selected app
Intent intent = new Intent();
intent.SetAction(Intent.ActionView);
intent.SetDataAndType(uri, mime);
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);
//context.StartActivity(intent);
Intent chooser = Intent.CreateChooser(intent, "Update");
chooser.SetFlags(ActivityFlags.NewTask);
IList<Android.Content.PM.ResolveInfo> resInfoList = context.PackageManager.QueryIntentActivities(chooser, Android.Content.PM.PackageInfoFlags.MatchDefaultOnly);
foreach (Android.Content.PM.ResolveInfo resolveInfo in resInfoList)
{
String packageName = resolveInfo.ActivityInfo.PackageName;
context.GrantUriPermission(packageName, uri, ActivityFlags.GrantWriteUriPermission | ActivityFlags.GrantReadUriPermission);
}
context.StartActivity(chooser);
Manifest:
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<application android:label="DRIVER" android:theme="@style/MainTheme" android:requestLegacyExternalStorage="true">
<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/file_paths"></meta-data>
</provider>
</application>
XML File:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />|
Device error logs:
12-21 19:18:50.371 pixel_5_-_api_30 Warning 13637 InstallStaging java.io.FileNotFoundException: open failed: EACCES (Permission denied)
at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:151)
at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:781)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1983)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1798)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1475)
at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:174)
at com.android.packageinstaller.InstallStaging$StagingAsyncTask.doInBackground(InstallStaging.java:167)
at android.os.AsyncTask$3.call(AsyncTask.java:394)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:305)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
12-21 19:18:50.371 pixel_5_-_api_30 Warning 13637 InstallStaging Error staging apk from content URI
12-21 19:18:50.370 pixel_5_-_api_30 Error 1332 MediaProvider Couldn't find file: /storage/emulated/0/Download/test2.apk