2013-07-25 49 views
2

下面是我的代碼,無法下載完整的文件在Android應用

從Activity類

Intent intent = new Intent(this, DownloadService.class); 
     // Create a new Messenger for the communication back 
     Messenger messenger = new Messenger(handler); 
     intent.putExtra("MESSENGER", messenger); 
     intent.setData(Uri.parse("http://www.abc.ezy.asia/E-MobApps/op.apk")); 
     intent.putExtra("urlpath", "http://www.abc.ezy.asia/E-MobApps/op.apk"); 
     startService(intent); 

我已經overrided服務類方法onHandle事件

// DownloadService Class 
@Override 
    protected void onHandleIntent(Intent intent) { 
     Uri data = intent.getData(); 
     String urlPath = intent.getStringExtra("urlpath"); 
     String fileName = data.getLastPathSegment(); 
     File output = new File(Environment.getExternalStorageDirectory(),fileName); 
     if (output.exists()) { 
      output.delete(); 
     } 
     InputStream stream = null; 
     FileOutputStream fos = null; 
     try { 
      URL url = new URL(urlPath); 
      stream = url.openConnection().getInputStream(); 
      fos = new FileOutputStream(output.getPath()); 
      byte dataB[] = new byte[1024]; 
      InputStreamReader reader = new InputStreamReader(stream); 
      int next = -1; 
      while ((next = reader.read()) != -1) { 
       fos.write(next); 
      } 
      fos.flush(); 
      result = Activity.RESULT_OK; 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (stream != null) { 
       try { 
        stream.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      if (fos != null) { 
       try { 
        fos.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

     Bundle extras = intent.getExtras(); 
     if (extras != null) { 
      Messenger messenger = (Messenger) extras.get("MESSENGER"); 
      Message msg = Message.obtain(); 
      msg.arg1 = result; 
      msg.obj = output.getAbsolutePath(); 
      try { 
       messenger.send(msg); 
      } catch (android.os.RemoteException e1) { 
       Log.w(getClass().getName(), "Exception sending message", e1); 
      } 

     } 
    } 
} 

在上面的代碼我使用文件流&輸入流閱讀器下載當試圖下載html文件時提交 ,然後完成文件被下載到我的SD卡。但是當我嘗試APK時。文件下載2.2 MB而不是2.4 MB解析問題在那裏。請幫助我解決問題。

+0

我不認爲你的代碼將根據區別對待的文件類型。請確保您知道APK的確切大小(可能爲2.2),或者文件可能已損壞。爲什麼不嘗試在網絡上使用其他一些apk文件? –

+0

謝謝。但是當我通過瀏覽器下載文件時,它完全下載 –

+0

,當您嘗試打開(安裝)從App下載的文件時會發生什麼情況?它表明它不完整或損壞? –

回答

1

嘗試這段代碼:

   URL url = new URL(fileUrl); 
       URLConnection connection = url.openConnection(); 
       connection.connect(); 
       int fileLength = connection.getContentLength(); 
       InputStream input = new BufferedInputStream(url.openStream());     
       OutputStream output = new FileOutputStream(output.getPath()); 
       byte data[] = new byte[1024]; 
       int count; 
       while ((count = input.read(data)) != -1) { 
        output.write(data, 0, count); 
       } 
       output.flush(); 
       input.close(); 
       result = Activity.RESULT_OK;  
+0

我也試過這個。但它沒有工作 –

+0

是你變得相同的大小? –

相關問題