Trying to update my apk programmatically results in parse error
<p>I need to update my application without Google Play. So I uploaded my apk to my server to be downloaded when needed.</p>

<p>It's downloaded fine by the following code, but when the installation intent is started it's throwing: "Parse Error: There is a problem parsing the package"</p>

<p>If I try to run the downloaded apk manually, it still gives the same error.</p>

<p>BUT, if I transfer the apk by USB and then run it, it installs smoothly. So this problem is not about the apk itself.</p>

<p>Here is my Task class:</p>

<pre><code>class Update implements Runnable {
    @Override
    public void run() {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(InetAddress.getByName("mysite.com"));
            ftpClient.login("mysite.com", "123456"));
            ftpClient.enterLocalPassiveMode();
            String remoteFile1 = "/httpdocs/program/app-release.apk";
            String downPath = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS).getAbsolutePath();
            File myapk = new File(downPath + "/app-release.apk");
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(myapk));
            boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
            outputStream1.close();
            myapk.setReadable(true, false);
            if (!success) {
                ftpClient.logout();
                ftpClient.disconnect();
                return;
            } else {
                Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                        .setDataAndType(Uri.fromFile(myapk), "application/vnd.android.package-archive")
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(promptInstall);
            }
        }
        catch (Exception ex) {
            //catch exception
        }
    }
}
</code></pre>