4

I'm downloading APK from server in string format, converting that to byte array and creating apk file in SD card. There is no issues upto this, if i try to install the same apk it showing an alert (parse error) message like

There is a problem parsing the package.

and in logcat

01-13 12:06:51.562: W/PackageParser(4364): Unable to read AndroidManifest.xml of /mnt/sdcard/example.apk
01-13 12:06:51.562: W/PackageParser(4364): java.io.FileNotFoundException: AndroidManifest.xml
01-13 12:06:51.562: W/PackageParser(4364): at android.content.res.AssetManager.openXmlAssetNative(Native Method)
01-13 12:06:51.562: W/PackageParser(4364): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:486)
01-13 12:06:51.562: W/PackageParser(4364): at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:454)
01-13 12:06:51.562: W/PackageParser(4364): at android.content.pm.PackageParser.parsePackage(PackageParser.java:401)
.............................................................
01-13 12:06:51.562: W/PackageParser(4364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-13 12:06:51.562: W/PackageParser(4364): at dalvik.system.NativeStart.main(Native Method)
01-13 12:06:51.562: W/PackageInstaller(4364): Parse error when parsing manifest. Discontinuing installation

any idea what i did wrong, or any other method to solve this.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
VKJ
  • 417
  • 2
  • 8
  • 17

2 Answers2

4

If you are using API level 9 or more, I think it is better to use DownloadManager to download your apk . So that Android will take care of downloading the file for you.

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • Thanks for your response nandeesh. But i can't use DownloadManager, because in my application i'm getting Apk in String format which is sent from server. – VKJ Jan 13 '12 at 07:09
  • then it might be related to string encoding or how you are storing. You could compare the stored apk with the one on server byte by byte. That might give you some clues – nandeesh Jan 13 '12 at 08:16
0

This is the perfect working code for download and install .apk file.

  public void downloadInstall(String apkurl){
          try {
                URL url = new URL(apkurl);
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();

                String PATH = Environment.getExternalStorageDirectory() + "/download/";
                File file = new File(PATH);
                file.mkdirs();
                File outputFile = new File(file, "app.apk");
                FileOutputStream fos = new FileOutputStream(outputFile);

                InputStream is = c.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(new    File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
         startActivity(intent); 

            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "download error!", Toast.LENGTH_LONG).show();
            }
      }  

Try this code i hope this will work.

himanshu
  • 1,990
  • 3
  • 18
  • 36