Android错误解析包 - 部分下载

时间:2017-05-06 19:24:58

标签: android android-install-apk android-internal-storage

我正在尝试在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. 我从Main Fragment调用上面列出的代码(我将其放入一个单独的类文件中)。应该从主要活动本身调用吗?
  4. 我正在尝试写入内部存储器而不是外部SD卡。这可能是问题吗?如果是这样 - 我如何将其写入内部存储器?
  5. 我尝试过使用“Environment.getExternalStorageDirectory()。getPath()”作为路径,但也没有用。有关问题,请参阅#4。

1 个答案:

答案 0 :(得分:0)

目前,我已决定将其置于架子上,并使用基于谷歌API的方法,我仍在研究。