安装.apk文件时出现Parse Error

时间:2013-11-25 08:41:16

标签: android android-download-manager

我在下载后使用DownloadManager从服务器下载.apk文件,当我尝试手动安装应用程序时,它提供了解析错误,并显示消息“解析包时出现问题”。

但是当我通过浏览器下载并复制到SD卡然后安装时,同样的apk,然后安装成功。

所以希望minsdkversion没有问题,我已经检查过Unknown Sources。

我的代码就是这样下载apk文件

        public class UpdateCheckerService extends Service {


    UpdateChecker update_checker;
    DownloadManager downloadManager;
    String Download_path="http://www.xxxxx.com/mobile_api/xxxxxx.apk";

    SharedPreferences preferenceManager;
    String Download_ID = "100";


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    public void onCreate()
    {
        System.out.println("SERVICE ************ CREATED");

    }
    public void onStart(Intent intent1, int start_id)
    {
        System.out.println("Service started");
        IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
         registerReceiver(downloadReceiver, intentFilter);
         System.out.println("Service started");

        if(isNetworkAvailable())
        {


            if(true)
            {
                System.out.println("Update AVAAILLL:");
                preferenceManager = PreferenceManager.getDefaultSharedPreferences(this);
                 downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
                 Environment
                  .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                  .mkdirs();

                   Uri Download_Uri = Uri.parse(Download_path);


                    DownloadManager.Request request = new DownloadManager.Request(Download_Uri);

                    long download_id = downloadManager.enqueue(request.setTitle("Downloading...")
                            .setDescription("xxxx").setMimeType("application/vnd.android.package-archive").setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
                                    "xxxxx.apk"));


                    Editor PrefEdit = preferenceManager.edit();
                    PrefEdit.putLong(Download_ID, download_id);
                    PrefEdit.commit();
            }
        }   
    }
    public void onDestroy()
    {

    }



    private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

          @Override
          public void onReceive(Context arg0, Intent arg1) {
           // TODO Auto-generated method stub
           DownloadManager.Query query = new DownloadManager.Query();
           query.setFilterById(preferenceManager.getLong(Download_ID, 0));
           Cursor cursor = downloadManager.query(query);

           if(cursor.moveToFirst()){
            int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
            int status = cursor.getInt(columnIndex);
            int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
            int reason = cursor.getInt(columnReason);

            if(status == DownloadManager.STATUS_SUCCESSFUL){
             //Retrieve the saved download id
             long downloadID = preferenceManager.getLong(Download_ID, 0);

             ParcelFileDescriptor file;

             try {
              file = downloadManager.openDownloadedFile(downloadID);
              Toast.makeText(UpdateCheckerService.this,
                "File Downloaded: " + file.toString(),
                Toast.LENGTH_LONG).show();

             } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              Toast.makeText(UpdateCheckerService.this,
                e.toString(),
                Toast.LENGTH_LONG).show();
             }

            }else if(status == DownloadManager.STATUS_FAILED){
             Toast.makeText(UpdateCheckerService.this,
               "FAILED!\n" + "reason of " + reason,
               Toast.LENGTH_LONG).show();
            }else if(status == DownloadManager.STATUS_PAUSED){
             Toast.makeText(UpdateCheckerService.this,
               "PAUSED!\n" + "reason of " + reason,
               Toast.LENGTH_LONG).show();
            }else if(status == DownloadManager.STATUS_PENDING){
             Toast.makeText(UpdateCheckerService.this,
               "PENDING!",
               Toast.LENGTH_LONG).show();
            }else if(status == DownloadManager.STATUS_RUNNING){
             Toast.makeText(UpdateCheckerService.this,
               "RUNNING!",
               Toast.LENGTH_LONG).show();
            }
           }
          }

         };

}

这是logcat

11-25 16:31:58.195: D/asset(3422): failed to open Zip archive '/mnt/sdcard/Download/SavariLocal.apk'
11-25 16:31:58.210: W/PackageParser(3422): Unable to read AndroidManifest.xml of /mnt/sdcard/Download/SavariLocal.apk
11-25 16:31:58.210: W/PackageParser(3422): java.io.FileNotFoundException: AndroidManifest.xml
11-25 16:31:58.210: W/PackageParser(3422):  at android.content.res.AssetManager.openXmlAssetNative(Native Method)
11-25 16:31:58.210: W/PackageParser(3422):  at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:486)
11-25 16:31:58.210: W/PackageParser(3422):  at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:454)
11-25 16:31:58.210: W/PackageParser(3422):  at android.content.pm.PackageParser.parsePackage(PackageParser.java:402)
11-25 16:31:58.210: W/PackageParser(3422):  at com.android.packageinstaller.PackageUtil.getPackageInfo(PackageUtil.java:79)
11-25 16:31:58.210: W/PackageParser(3422):  at com.android.packageinstaller.PackageInstallerActivity.onCreate(PackageInstallerActivity.java:242)
11-25 16:31:58.210: W/PackageParser(3422):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-25 16:31:58.210: W/PackageParser(3422):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-25 16:31:58.210: W/PackageParser(3422):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-25 16:31:58.210: W/PackageParser(3422):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-25 16:31:58.210: W/PackageParser(3422):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-25 16:31:58.210: W/PackageParser(3422):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 16:31:58.210: W/PackageParser(3422):  at android.os.Looper.loop(Looper.java:130)
11-25 16:31:58.210: W/PackageParser(3422):  at android.app.ActivityThread.main(ActivityThread.java:3687)
11-25 16:31:58.210: W/PackageParser(3422):  at java.lang.reflect.Method.invokeNative(Native Method)
11-25 16:31:58.210: W/PackageParser(3422):  at java.lang.reflect.Method.invoke(Method.java:507)
11-25 16:31:58.210: W/PackageParser(3422):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-25 16:31:58.210: W/PackageParser(3422):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-25 16:31:58.210: W/PackageParser(3422):  at dalvik.system.NativeStart.main(Native Method)
11-25 16:31:58.210: W/PackageInstaller(3422): Parse error when parsing manifest. Discontinuing installation

请建议我如何解决这个问题,以及我错在哪里..

提前致谢。

2 个答案:

答案 0 :(得分:0)

我有同样的问题,我发现在卸载应用程序后重新安装了apk帮助。

another solution我没有尝试,但它可能有用。

答案 1 :(得分:0)

我以前遇到过这个问题。这就是我调试它的方式。首先,如果出现错误消息,请通过终端adb install path_to_your_app / name_of_your_app.apk安装APK。adb:无法安装name_of_your_app.apk:失败[INSTALL_FAILED_NO_MATCHING_ABIS:无法提取本机库,这意味着您要构建的体系结构与仿真器架构。

由于这是来自外部来源的APK,因此您可能需要考虑对其进行重新包装并根据设备架构进行构建。您可以使用以下工具:https://ibotpeaches.github.io/Apktool/

希望这会有所帮助