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

时间:2018-01-15 20:16:00

标签: android android-fileprovider

我想在我的应用程序中安装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)

为什么会发生这种错误?

3 个答案:

答案 0 :(得分:3)

您的FileProvider配置错误。您的FileProvider元数据需要<external-path>个元素,而您没有。{1}}元素。请参阅the documentation

答案 1 :(得分:2)

我测试了以下代码。

在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="/"/>

答案 2 :(得分:0)

根据EXTRA_NOT_UNKNOWN_SOURCE上的Android文档:

  

要使其正常工作,必须使用startActivityForResult()启动安装程序。