以编程方式在 Android N 中安装应用程序

Install application in Android N programmatically

我想在我的应用程序中安装一个 apk,我正在使用这种方法,但我总是收到下面列出的错误:

代码

 File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.apk");
 Uri fileUri = Uri.fromFile(file);
 if (Build.VERSION.SDK_INT >= 24) {
     fileUri = FileProvider.getUriForFile(DLActivity.this, "codenevisha.com.apps.bartarinapp.provider",file);
 }
 Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
 intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
 intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive");
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 startActivity(intent);

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="codenevisha.com.apps.bartarinapp">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:name=".utils.G"
        android:allowBackup="true"
        android:icon="@mipmap/logo_chabok"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="codenevisha.com.apps.bartarinapp.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/paths"/>
        </provider>


        <activity
            android:name=".activity.ActivitySplash"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".activity.DLActivity">
        </activity>
    </application>

</manifest>

我使用了这个 path.xml 文件:

<external-files-path
    name="share"
    path="."/> 

但是当我 运行 我的应用程序收到此错误时:

Process: codenevisha.com.apps.bartarinapp, PID: 21392
   java.lang.RuntimeException: Unable to start activity ComponentInfo{codenevisha.com.apps.bartarinapp/codenevisha.com.apps.bartarinapp.activity.DLActivity}: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/test.apk
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
       at android.app.ActivityThread.-wrap14(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6776)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
    Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/test.apk
       at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
       at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
       at codenevisha.com.apps.bartarinapp.activity.DLActivity.onCreate(DLActivity.java:46)
       at android.app.Activity.performCreate(Activity.java:6955)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)

为什么会出现这个错误?

您的 FileProvider 配置错误。您的 FileProvider 元数据需要一个 <external-path> 元素,而您没有。参见 the documentation

我测试了下面的代码。

在 Menifest 中添加以下权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

下面是安装包的代码

//// TODO: 1/16/18 Check the external storage permission 
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.apk");
            Uri fileUri = Uri.fromFile(file);
            if (Build.VERSION.SDK_INT >= 24) {
                String packageId = getApplicationContext().getPackageName();
                fileUri = FileProvider.getUriForFile(MainActivity.this, packageId + ".files", file);
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
            intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);

Menifest 中的提供者

   <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.files"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

file_paths.xml

<paths>
<external-path name="external" path="/"/>

根据 EXTRA_NOT_UNKNOWN_SOURCE 上的 Android 文档:

For this to work you must start the installer with startActivityForResult().