Android 以编程方式安装 apk 错误 - 包解析错误 [英] Android install apk programmatically error - Package Parse error

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

问题描述

我在我的页面上创建了一个 Edittext,以允许用户通过下载 apk 并安装来更新应用程序.我正在使用 loojp 库来处理异步任务.

I create an Edittext at my page, to allow user to update the apps by downloading the apk and install. I am using loojp library to handle the Asynctask.

我在活动中的代码:

f0textupdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            try {
                view.setEnabled(false);
                final ProgressBar f0progress = (ProgressBar) findViewById(R.id.f0progress);
                f0progress.setProgress(0);
                mHttpClient.get("http://google.com/apps.apk", new FileAsyncHttpResponseHandler(lastcreated) {

                    @Override
                    public void onSuccess(int i, Header[] headers, File file) {
                        //sendBroadcast(new Intent(Intent.ACTION_INSTALL_PACKAGE, Uri.fromFile(file)));
                        try {
                            String path = file.getParent()+"/"+ CfgMain.siteid+ ".apk";
                            File dst = new File(path);
                            if (!dst.exists()) {
                                try {
                                    dst.createNewFile();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                            InputStream in = new FileInputStream(file);
                            OutputStream out = new FileOutputStream(dst);

                            // Transfer bytes from in to out
                            byte[] buf = new byte[1024];
                            int len;
                            while ((len = in.read(buf)) > 0) {
                                out.write(buf, 0, len);
                            }
                            in.close();
                            out.close();

                            Intent installIntent = new Intent();
                            installIntent.setAction(Intent.ACTION_INSTALL_PACKAGE);
                            installIntent.setDataAndType(Uri.fromFile(dst), "application/vnd.android.package-archive");
                            installIntent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
                            installIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
                            installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(installIntent);

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(int i, Header[] headers, Throwable throwable, File file) {
                        if (file.exists()) {
                            file.delete();
                        }
                    }

                    @Override
                    public void onProgress(long bytesWritten, long totalSize) {
                        int totProgress = (int) (((float) bytesWritten * 100) / totalSize);
                        if (totProgress > 0) {
                            f0progress.setVisibility(View.VISIBLE);
                            f0progress.setProgress(totProgress);
                        }
                    }

                });
            } catch (Exception ex) {

            }

        }
    });

Manifest.xml

Manifest.xml

   <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

提示Package parse error"错误信息.

It prompt an error message "Package parse error".

1) 我是否需要在下载后按照原始 apk 名称转换文件名,因为文件文件"似乎存储为没有扩展名的临时名称

1) do I need to convert the filename as per the original apk name after download, as the "File file" seem storing as a temporarily name without extension

更新 1来自 logCat 的错误:

Update 1 Error from logCat:

05-17 11:17:19.142 4562-4562/?W/zipro:无法打开zip '/data/data/com.M28/cache/77lori.apk':权限被拒绝

05-17 11:17:19.142 4562-4562/? W/zipro﹕ Unable to open zip '/data/data/com.M28/cache/77lori.apk': Permission denied

05-17 11:17:19.152 4562-4562/?W/PackageParser﹕无法读取/data/data/com.MMM/cache/MM.apk的AndroidManifest.xmljava.io.FileNotFoundException: AndroidManifest.xml

05-17 11:17:19.152 4562-4562/? W/PackageParser﹕ Unable to read AndroidManifest.xml of /data/data/com.MMM/cache/MM.apk java.io.FileNotFoundException: AndroidManifest.xml

更新 1.1- 更改 Onsuccess()

Update 1.1 - change the Onsuccess()

  • 在 Manifest.xml 中添加额外的权限

推荐答案

Ok found a 解决方案 来自其他论坛(xamarin)并解决我的问题..您需要将 APK 保存到公共目录.

Ok found a solution from other forum(xamarin) and solve my problem.. You need to save the APK to a public directory.

例如:

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/MM.apk";

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

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