无法在 Android 中打开以编程方式下载的文件。重启Android台设备后即可打开。可能是什么问题?

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

在 Android 应用程序中,开发于 Java,我从网络服务下载了一个 apk。将文件下载到移动设备(平板电脑)后,我无法通过单击打开文件。当我尝试打开下载的文件时收到“错误”消息。 但是如果我使用 Android 系统复制功能复制文件,我可以打开复制的版本。 如果我重新启动 Android 设备,可以打开下载的文件。

下载文件可能是什么问题?

这是下载文件class。 public class NewAppDownloader 扩展了 AsyncTask {

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;
}

}

这是onclick事件中的调用代码

public void onClick(View v) {

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


    }

提前感谢所有帮助和回答。

这是设计使然。请参阅 alternative distribution 上的开发人员页面,特别是关于通过网站分发的部分。复制在此供参考:

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.

如果您的设备已禁用该安全限制,则可能是您用来打开下载的 APK 的任何应用都无法将其识别为该格式。您使用什么应用程序打开它,显示的错误消息是什么?它在重新启动时打开的原因可能是由于 OS 扫描并将其识别为 APK,这样无论您使用什么应用程序打开它都会成功安装它。复制下载的文件也可以触发相同的过程。

此外,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 :)