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

AtlantisDe 41 Reputation points
2021-09-23T16:40:06.343+00:00

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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 33,091 Reputation points Microsoft Vendor
    2021-09-24T09:24:49.877+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.