Android PackageInstaller安装失败\u内部错误:会话已放弃

Android PackageInstaller安装失败\u内部错误:会话已放弃,android,packageinstaller,Android,Packageinstaller,我在尝试使用软件包安装程序安装apk时遇到问题。安装失败,状态消息非常模糊:“安装失败\u内部错误:会话放弃” 以下是安装软件包的代码: public static void installPackage(Context context, String packageName, String apkPath) { Logger.debug("Installing package " + apkPath); PackageInstaller packageIns

我在尝试使用软件包安装程序安装apk时遇到问题。安装失败,状态消息非常模糊:“安装失败\u内部错误:会话放弃”

以下是安装软件包的代码:

public static void installPackage(Context context, String packageName, String apkPath)
{
    Logger.debug("Installing package " + apkPath);
    PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();

    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);

    try {
        Logger.debug("Creating session");
        int sessionId = packageInstaller.createSession(params);
        PackageInstaller.Session session = packageInstaller.openSession(sessionId);

        Logger.debug("Copying buffer");
        OutputStream out = session.openWrite(packageName, 0, -1);
        FileInputStream fileInputStream = new FileInputStream(new File(apkPath));

        byte[] buffer = new byte[1024];
        int len = fileInputStream.read(buffer);
        while(len != -1)
        {
            out.write(buffer,0, len);
            len = fileInputStream.read(buffer);
        }

        session.fsync(out);
        fileInputStream.close();
        out.close();

        Logger.debug("Buffer copied, sending intent");

        Intent intent = new Intent(INSTALL_COMPLETE_ACTION);
        IntentSender intentSender = PendingIntent.getBroadcast(context, sessionId,
                intent, 0) .getIntentSender();
        session.commit(intentSender);
        session.close();
    }
    catch(IOException e)
    {
        Logger.error("IOException while installing package");
        Logger.error(e);
    }
    catch(Exception e)
    {
        Logger.error("Unknown exception while installing package");
        Logger.error(e);
    }
}
以下是处理意图的广播接收器:

private BroadcastReceiver mInstallReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        Logger.debug("Received intent");
        Logger.debug("Intent action: " + intent.getAction());
        int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE);
        Logger.debug("Intent status: " + status);
        String message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
        Logger.debug("Status message: " + message);

        if(status == PackageInstaller.STATUS_PENDING_USER_ACTION)
        {
            Intent innerIntent = (Intent) intent.getParcelableExtra(Intent.EXTRA_INTENT);
            startActivity(innerIntent);
        }
    }
};
下面是broadcastreceiver生成的日志:

11-12 15:22:08.364  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:08.365  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:08.365  7179  7179 D DummyActivity @ onReceive()        Intent status: -1
11-12 15:22:08.365  7179  7179 D DummyActivity @ onReceive()        Status message: null
11-12 15:22:08.378  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:08.378  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:08.379  7179  7179 D DummyActivity @ onReceive()        Intent status: -1
11-12 15:22:08.379  7179  7179 D DummyActivity @ onReceive()        Status message: null
11-12 15:22:08.387  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:08.387  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:08.387  7179  7179 D DummyActivity @ onReceive()        Intent status: -1
11-12 15:22:08.388  7179  7179 D DummyActivity @ onReceive()        Status message: null
11-12 15:22:09.155  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.155  7179  7179 D DummyActivity @ onReceive()        Intent status: 1
11-12 15:22:09.155  7179  7179 D DummyActivity @ onReceive()        Status message: INSTALL_FAILED_INTERNAL_ERROR: Session relinquished
11-12 15:22:09.155  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:09.155  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.156  7179  7179 D DummyActivity @ onReceive()        Intent status: 1
11-12 15:22:09.156  7179  7179 D DummyActivity @ onReceive()        Status message: INSTALL_FAILED_INTERNAL_ERROR: Session relinquished
11-12 15:22:09.156  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:09.157  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.157  7179  7179 D DummyActivity @ onReceive()        Intent status: 1
11-12 15:22:09.157  7179  7179 D DummyActivity @ onReceive()        Status message: INSTALL_FAILED_INTERNAL_ERROR: Session relinquished
11-12 15:22:09.214  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:09.214  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.215  7179  7179 D DummyActivity @ onReceive()        Intent status: 4
11-12 15:22:09.215  7179  7179 D DummyActivity @ onReceive()        Status message: INSTALL_PARSE_FAILED_NOT_APK: Failed parse during installPackageLI: Failed to parse /data/app/vmdl1253852664.tmp
11-12 15:22:09.215  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:09.215  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.215  7179  7179 D DummyActivity @ onReceive()        Intent status: 4
11-12 15:22:09.216  7179  7179 D DummyActivity @ onReceive()        Status message: INSTALL_PARSE_FAILED_NOT_APK: Failed parse during installPackageLI: Failed to parse /data/app/vmdl1253852664.tmp
11-12 15:22:09.216  7179  7179 D DummyActivity @ onReceive()        Received intent
11-12 15:22:09.216  7179  7179 D DummyActivity @ onReceive()        Intent action: net.test.INSTALL_COMPLETE
11-12 15:22:09.216  7179  7179 D DummyActivity @ onReceive()        Intent status: 4
11-12 15:22:09.216  7179  7179 D DummyActivity @ onReceive()        Status message: INSTALL_PARSE_FAILED_NOT_APK: Failed parse during installPackageLI: Failed to parse /data/app/vmdl1253852664.tmp
提前谢谢