Downloading the APK from the server and installing it on the device

I download the APK from the server in string format, converting it to an array of bytes and creating an apk file on the SD card. There is no problem before that if I try to install the same apk that shows a warning (parsing) message, for example

There is a problem parsing the package.

and in logcat

01-13 12: 06: 51.562: W / PackageParser (4364): unable to read AndroidManifest.xml / mnt / sdcard / example.apk file
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): on 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β–Ί97)
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 during manifest parsing. Termination of installation

any idea that I did wrong, or any other method to solve this problem.

+4
source share
2 answers

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

+4
source

This is the perfect working code to download and install the .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 works.

0
source

Source: https://habr.com/ru/post/1390778/


All Articles