首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Jelly Bean android上出现“使用intent安装apk”的解析错误

在Jelly Bean android上出现“使用intent安装apk”的解析错误
EN

Stack Overflow用户
提问于 2014-10-28 15:41:57
回答 1查看 695关注 0票数 0

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

代码语言:javascript
复制
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 versions.If。我在jelly bean中尝试了一下,我得到了"There a parse error in a package error“,请帮助我解决这个问题

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-06-16 17:15:59

您需要对您的apk文件授予读取权限。在install apk file函数中,添加:

代码语言:javascript
复制
File file = new File(Environment.getExternalStorageDirectory() + "/download/" + "yourapp.apk")
file.setReadable(true, false);
installer.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26603085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档