'Programatically downloaded file can not be opened in Android. It can be opened after I restart Android device. What may be the problem?

In an Android aplication, developed in Java, I download an apk from a web service. After downloading file to the mobile device (tablet) I can not open the file by clicking on it. I get "error" message when I try to open the downloaded file. But If I copy the file using Android system copy function I can open the copied version. If I restart the Android device the downloaded file can be opened.

What can be the problem in downloading file?

This is the class that downloads file. public class NewAppDownloader extends AsyncTask<String, String, String> {

Context context;
public NewAppDownloader(Context aContext) {
    this.context=aContext;
}

protected String doInBackground(String... params) {

    String downloadedAppFilePath= params[1];

    HttpURLConnection connection = null;
    BufferedReader bufferedReader = null;
    FileOutputStream fileOutputStream = null;

    try {

        URL url = new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "application/apk");
        connection.connect();
        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            // create directory
            File dir = new File(downloadedAppFilePath);
            dir.mkdirs();

            InputStream inputStream = connection.getInputStream();
            fileOutputStream = new FileOutputStream(downloadedAppFilePath + "/my.apk");

            byte[] b = new byte[1024];

            int len = 0;
            while ((len = inputStream.read(b, 0, b.length)) != -1) {
                fileOutputStream.write(b, 0, len);
            }

            fileOutputStream.flush();
            fileOutputStream.close();
            inputStream.close();

        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    return "Download completed.";

}
protected void onPostExecute (String s){

    context=null;
}

}

This is the calling code in an onclick event

public void onClick(View v) {

    (new NewAppDownloader(getContext())).execute(appDownloadConnection,downloadedAppFilePath);


    }

Thanks in advance for all help and answers.



Solution 1:[1]

This is by design. Refer to the developer page on alternative distribution, specifically the section on distributing through a website. Copied here for reference:

If you don't want to release your apps on a marketplace such as Google Play, you can make them available for download on your website or server, including on a private or enterprise server. To do this, first prepare your apps for release in the normal way, then host the release-ready APK files on your website and provide users with a download link. To install an app distributed in this way, users must opt-in for installing unknown apps.

If your device already has that security restriction disabled, it might be that whatever app you are using to open the downloaded APK does not recognize it as that format. What app are you using to open it and what is the error message displayed? The reason it opens upon restart is likely due to the OS scanning and identifying it as an APK, such that whatever app you use to open it installs it successfully. Copying the downloaded file could also trigger the same process.

Also, AsyncTask has been deprecated. Consider using an Executor or some other modern concurrency pattern instead. Check out Processes and Threads for alternatives or switch to Kotlin for even easier concurrency with Coroutines :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zen_of_kermit