0

什么是我的代码的问题:Android的下载文件无法解析包解析错误

@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 

我的代码问题是什么?
在此先感谢

+0

这是您的通知还是由系统提供? – hoomi 2014-08-30 07:13:31

+0

@hoomi它是由系统,因为我下载了一个apk文件。我认为问题出在我的'doInBackground'方法上。 – 2014-08-30 07:26:28

+0

您是否在LogCat中遇到任何异常或警告? – hoomi 2014-08-30 07:38:33

回答

0

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