代码之家  ›  专栏  ›  技术社区  ›  Naruto Uzumaki

android下载文件无法解析包解析错误

  •  0
  • Naruto Uzumaki  · 技术社区  · 11 年前

    我的代码问题是什么:

    @Override
        protected String doInBackground(String... sUrl) {
    
            InputStream input = null;
    
            URLConnection conection = null;
            BufferedOutputStream bout = null;
            FileOutputStream fos = null;
    
            int downloaded = 0;
    
            try {
                URL url = new URL(sUrl[0]);
                conection = url.openConnection();
                //conection.connect();
    
                int lenghtOfFile = conection.getContentLength();
    
    
                conection = null;
                conection = url.openConnection();
    
                if(STATUS) {
                    File file = new File(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk");
                    if (file.exists()) {
                        downloaded = (int) file.length();
                        conection.setRequestProperty("Range", "bytes=" + (file.length()) + "-");
                    }
                }
                else {
                    conection.setRequestProperty("Range", "bytes=" + downloaded + "-")
                }
    
                conection.setDoInput(true);
                conection.setDoOutput(true);
    
    
    
                input = new BufferedInputStream(url.openStream(), 8192);
    
                fos=(downloaded==0)? new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk"): new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk",true);
                bout = new BufferedOutputStream(fos, 1024);
    
    
    
                byte data[] = new byte[1024];
    
                long total = 0;
                int count = 0;
    
                while ((count = input.read(data, 0, 1024)) >= 0) {
                    if (isCancelled()) {
                        input.close();
                        return null;
                    }
    
                    bout.write(data, 0, count);
                    downloaded += count;
                    publishProgress((int)(downloaded * 100/ lenghtOfFile) );
                    total += count;
                }
    
            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            } finally {
                try {
                    if (output != null)
                        output.close();
                    if (input != null)
                        input.close();
                    if (fos != null)
                        fos.close();
                    if (bout != null)
                        bout.close();
                } catch (IOException ignored) {
                }
    
                if (conection != null)
                    conection = null;
            }
    
            return null;
        }
    

    我在单击时将此启动器设置为通知栏:

        notification = new NotificationCompat.Builder(MainActivity.this)
                .setSmallIcon(R.drawable.down_icon)
                .setOngoing(true);
    
    
    
        resultIntent = new Intent(MainActivity.this, DownloadsActivity.class);
    
        PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(resultPendingIntent);
    

    当文件完全下载并单击通知栏时,我看到以下错误:

    There is a problem parsing the package
    

    我的代码问题是什么?
    提前感谢

    1 回复  |  直到 11 年前
        1
  •  0
  •   hoomi    11 年前

    你试过在while循环后添加bout.flush()吗?也许并不是所有的字节都写入了文件