user1099280
user1099280

Reputation: 15

Parse error “installing an apk using intent” on Jelly Bean android

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);
    }



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);
    }

The above works well upto 4.0 versions.If i try it in the jelly bean I am getting "There is a parse error in a package error" Please help me to solve this issues

Thanks.

Upvotes: 0

Views: 743

Answers (1)

Saurabh
Saurabh

Reputation: 1065

You need to give read permission to your apk file. In the install apk file function, add:

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

Upvotes: 1

Related Questions