0

我正在尝试在 Google Play 商店之外下载并安装我的 android 的更新。使用本教程作为指南 ( http://simpledeveloper.com/how-to-update-android-apk-outside-the-playstore/ ) 我在我的应用程序中插入了代码来下载我的应用程序的 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. 我已经制作了签名和未签名的apks。两者都无法下载完整的应用程序。
  2. 我将 apk 的 api 级别设置为设备的相同 api 级别(在 gradle 构建文件中。我使用的是 Android Studio)。Android版本是5.1.1(一定是项目原因)。
  3. 我从主片段调用上面列出的代码(我放入一个单独的类文件中)。应该从主要活动本身调用它吗?
  4. 我正在尝试写入内部存储器而不是外部 SD 卡。这可能是问题吗?如果是这样 - 我如何将其写入内部存储器?
  5. 我也尝试使用“Environment.getExternalStorageDirectory().getPath()”作为路径,但这也不起作用。有关问题,请参阅#4。
4

1 回答 1

0

现在,我决定把它放在架子上,并考虑使用我仍在研究的基于谷歌 API 的方法。

于 2017-07-24T12:07:50.377 回答