以编程方式安装 APK 时解析错误 [英] Parse error when programmatically installing an APK

查看:23
本文介绍了以编程方式安装 APK 时解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为应用创建一种机制,通过从应用内下载和安装更新的 APK 来更新自身.

I am trying to create a mechanism for an app to update itself by downloading and installing a later APK from within the app.

我有一个位于服务器上的 APK,如果我只是导航到 URI 然后打开 .apk 文件,它安装得很好.当我尝试以编程方式安装它时,问题就出现了.我收到解析错误 - 解析包时出现问题"

I have an APK located on a server which installs fine if I simply navigate to the URI then open the .apk file. The problem comes when I try to install it programmatically. I get "Parse Error - There was a problem while parsing the package"

目标手机允许从未知来源安装,并且在 AndroidManifest.xml 中我请求这些权限:

The target phone allows install from unknown sources and within AndroidManifest.xml I request these permissions:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.REQUEST_WRITE_PERMISSION"/>

执行更新的代码取自 StackOverflow 上的另一个线程,并稍作更改以适应我的特定情况.

The code to perform the update has been taken from another thread here on StackOverflow and changed slightly to suit my particular situation.

public class UpdateApp extends AsyncTask<String,Void,Void> {
    private Context context;
    public void setContext(Context contextf){
        context = contextf;
    }

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            URL url = new URL(arg0[0]);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);
            conn.connect();

            File file = context.getCacheDir();
            file.mkdirs();
            File outputFile = new File(file, "update.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = conn.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);


        } catch (Throwable ex) {
            Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
        }
        return null;
    }
}

我可以尝试什么来理解为什么从代码中安装 APK 时会产生错误,但从服务器下载时却没有问题?

What can I try in order to understand why the APK is generating an error when installed from within the code but installs without issue when downloaded from the server?

该应用是为 API 23 构建的,但在完成后需要使用 API 24.

The app is being built for API 23 although it will be required to work with API 24 once complete.

推荐答案

您必须使缓存的 apk 文件世界可读.

You will have to make your cached apk file world-readable.

is.close();

outputFile.setReadable(true, false);

这篇关于以编程方式安装 APK 时解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆