Android 解析包时出错 - 部分下载

Android Error parsing package - partial download

我正在尝试在 Google Play 商店之外为我的 android 下载并安装更新。 使用本教程作为指南 (http://simpledeveloper.com/how-to-update-android-apk-outside-the-playstore/) 我在我的应用程序中插入代码以从我的 google 驱动器下载我的应用程序的 apk 文件(具有更新)。

该应用程序只下载部分文件(可能是前 14KB 到 100KB)。

我做错了什么?

这是我的代码:

public class ApkUpdateTask extends AsyncTask <String,Void,Void>{
    private Context context;
    public void setContext(Context contextf){
        context = contextf;
    }

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            URL url = new URL(arg0[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = "/storage/emulated/0/Download/";
            //String PATH = Environment.getExternalStorageDirectory().getPath();
            File file = new File(PATH);
            //file.mkdirs();
            File outputFile = new File(file, "sonandso.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1;
            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().getPath())), "blahblah.apk");
            intent.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/Download/")), "blahblah.apk");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
            context.startActivity(intent);


        } catch (Exception e) {
            Log.e("UpdateAPP", "Update error! " + e.getMessage());
        }
        return null;
    }
}

备注:

  1. 我已经制作了签名和未签名的 apk。两者都无法下载完整的应用程序。
  2. 我将 apk 的 api 级别设置为设备的相同 api 级别(在 gradle 构建文件中。我正在使用 Android Studio ). Android版本是5.1.1(应该是项目的原因)。
  3. 我正在从主片段调用上面列出的代码(我将其放入单独的 class 文件中)。这应该从主 activity 本身调用吗??
  4. 我正在尝试写入内部存储器而不是外部 SD 卡。这可能是问题所在吗?如果是这样 - 我怎样才能将它写入内部存储器?
  5. 我也尝试过使用 "Environment.getExternalStorageDirectory().getPath()" 作为路径,但也没有用。有关问题,请参阅 #4。

目前,我已决定将其搁置并考虑使用我仍在研究的基于 google API 的方法。