Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 解析错误“;使用intent&x201D安装apk;关于果冻豆机器人_Android_Android Intent_Android 4.2 Jelly Bean_Android Install Apk - Fatal编程技术网

Android 解析错误“;使用intent&x201D安装apk;关于果冻豆机器人

Android 解析错误“;使用intent&x201D安装apk;关于果冻豆机器人,android,android-intent,android-4.2-jelly-bean,android-install-apk,Android,Android Intent,Android 4.2 Jelly Bean,Android Install Apk,我正在生成一个android应用程序,它能够在android应用程序中包含无线更新。因此,我将生成一个Web服务来获取versioncode,以便比较已安装应用程序的versioncode,如果它较小,那么我将触发从服务器安装更新,为此,我使用以下代码 String PATH = Environment.getExternalStorageDirectory() + "/download/"; File file = new File(PATH);

我正在生成一个android应用程序,它能够在android应用程序中包含无线更新。因此,我将生成一个Web服务来获取versioncode,以便比较已安装应用程序的versioncode,如果它较小,那么我将触发从服务器安装更新,为此,我使用以下代码

String PATH = Environment.getExternalStorageDirectory() + "/download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new  File(file, "yourapp.apk");
            downloadFile(file_url, outputFile);
            installApk();

//downloadfile function
private static void downloadFile(String url, File outputFile) {
        try {
            URL u = new URL(url);
            URLConnection conn = u.openConnection();
            int contentLength = conn.getContentLength();

            DataInputStream stream = new DataInputStream(u.openStream());

            byte[] buffer = new byte[contentLength];
            stream.readFully(buffer);
            stream.close();

            DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
            fos.write(buffer);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("FileNotFoundException",e+"");
            return; 
        } catch (IOException e) {
            Log.e("IOException",e+"");
            return; 
        }
    }

//install apk file function

private void installApk(){
        Intent installer = new Intent();
        installer.setAction(android.content.Intent.ACTION_VIEW);

        installer.putExtra(Intent.ACTION_PACKAGE_REPLACED, "org.wannatrak.android");


        installer.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "yourapp.apk")), "application/vnd.android.package-archive");

        installer.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);




        this.startActivity(installer);
    }



I am generating an android application which capable to include over-the-air updation in the android application. So that I am generating a Webservice for getting the versioncode so that I will compare the versioncode of installed application if it is lesser then I will trigger there is an update to install from the server, for this I using below code.

String PATH = Environment.getExternalStorageDirectory() + "/download/";
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new  File(file, "yourapp.apk");
            downloadFile(file_url, outputFile);
            installApk();

//downloadfile function
private static void downloadFile(String url, File outputFile) {
        try {
            URL u = new URL(url);
            URLConnection conn = u.openConnection();
            int contentLength = conn.getContentLength();

            DataInputStream stream = new DataInputStream(u.openStream());

            byte[] buffer = new byte[contentLength];
            stream.readFully(buffer);
            stream.close();

            DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
            fos.write(buffer);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("FileNotFoundException",e+"");
            return; 
        } catch (IOException e) {
            Log.e("IOException",e+"");
            return; 
        }
    }

//install apk file function

private void installApk(){
        Intent installer = new Intent();
        installer.setAction(android.content.Intent.ACTION_VIEW);

        installer.putExtra(Intent.ACTION_PACKAGE_REPLACED, "org.wannatrak.android");


        installer.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "yourapp.apk")), "application/vnd.android.package-archive");

        installer.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);




        this.startActivity(installer);
    }
上面提到的在4.0版本中都能很好地工作。如果我在jelly bean中尝试它,我会得到“包错误中存在解析错误”,请帮助我解决这个问题


谢谢。

您需要授予apk文件的读取权限。在安装apk文件功能中,添加:

File file = new File(Environment.getExternalStorageDirectory() + "/download/" + "yourapp.apk")
file.setReadable(true, false);
installer.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");