question

92696164 avatar image
0 Votes"
92696164 asked WenyanZhang-MSFT commented

How to run an apk file in SD card ? this is my code it do not work ..

How to run an apk file in SD card ? this is my code it do not work ..

code



Intent install = new Intent(Intent.ActionView);
install.SetFlags(ActivityFlags.NewTask);                            
install.SetDataAndType(Android.Net.Uri.FromFile(new Java.IO.File($"/storage/emulated/0/demo.apk")), "application/vnd.android.package-archive");
My.MainActivity.StartActivity(install);



i have add the permission

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_USERS" />


<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INSTANT_APP_FOREGROUND_SERVICE" />


ths very much




dotnet-xamarinforms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.

0 Votes 0 ·

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered

Hello,
Welcome to our Microsoft Q&A platform!
You could check the file path, If there is a apk in SDCard, you can run and install it. You could have a try with the folling steps:
1. Add FileProvider configuration in AndroidManifest

 <application android:label="AndroidRunApkDemo.Android" android:theme="@style/MainTheme">
         <provider android:name="androidx.core.content.FileProvider" android:authorities="com.companyname.androidrunapkdemo.fileprovider" android:exported="false" android:grantUriPermissions="true">
             <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" />
         </provider>
     </application>

2. Create a new xml/filepaths.xml in the Resources directory

 <paths xmlns:android="http://schemas.android.com/apk/res/android">
     <external-path name="external" path=""/>“
 </paths>

3. Use FileProvider to install apk compatible

 public void ApkInstall()
         {
             Intent install = new Intent(Intent.ActionView);
             String sdcardRoot = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
             String apkSavePath = sdcardRoot + "/com.companyname.app41.apk";
    
             Java.IO.File apkFile = new Java.IO.File(apkSavePath);
             install.SetFlags(ActivityFlags.NewTask);
    
             if (Build.VERSION.SdkInt >= BuildVersionCodes.N)
             {
                 var flags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
                 install.SetFlags(flags);
                 Android.Net.Uri uri = FileProvider.GetUriForFile(Android.App.Application.Context, Android.App.Application.Context.PackageName + ".fileprovider", apkFile);
                 install.SetDataAndType(uri, "application/vnd.android.package-archive");
             }
             else
             {
                 install.SetDataAndType(Android.Net.Uri.FromFile(apkFile), "application/vnd.android.package-archive");
             }
             Forms.Context.StartActivity(install);
         }

I use DependencyService to test, you can refer to the follwing code

XAML

  private void Button_Clicked(object sender, EventArgs e)
         {
             DependencyService.Get<ISDCardService>().ApkInstall();
    
         }

interface

 namespace AndroidRunApkDemo
 {
    public interface ISDCardService
     {
        void ApkInstall();
     }
 }

Android

 [assembly: Dependency(typeof(SDCardService))]
 namespace AndroidRunApkDemo.Droid
 {
     public class SDCardService : ISDCardService
     {
         public void ApkInstall()
         {
 .......
            }
 }

In addition, you may need this permission <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> after Android 8.
134890-image.png
Best Regards,
Wenyan Zhang


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



image.png (12.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.