2013-03-02 64 views
0

我很努力使一個簡單的應用程序來下載互聯網的文件繼承人我的代碼我從另一個問題編輯了一點,但我仍然沒有得到響應錯誤,每當我運行我的應用程序可以任何人顯示我的糾正,甚至一個不同的方式?努力使用Android Eclipse下載文件?

package com.example.downloading; 

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import org.apache.http.util.ByteArrayBuffer; 

import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.Handler; 
import android.os.Message; 
import android.os.ResultReceiver; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.app.Activity; 
import android.app.DownloadManager; 
import android.app.IntentService; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 

public class MainActivity extends Activity { 

    Button startBtn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     startBtn = (Button)findViewById(R.id.download_button); 
     startBtn.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 

       String url = "my stuff"; 
       DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
       request.setDescription("descrition"); 
       request.setTitle("title"); 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
        request.allowScanningByMediaScanner(); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       } 
       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "etc.mp3"); 


       DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
       manager.enqueue(request); 

      } 
     }); 
    } 
} 

回答

0
public void DownLoadTheMusic(){ 
     try { 
      URL url = new URL("http://www.tagsinfosoft.com/android/shelf/Shelf_Cam.mp3"); 
      HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); 

      String PATH = Environment.getExternalStorageDirectory() + "/download/"; 
      File file = new File(PATH); 
      file.mkdirs(); 
      File outputFile = new File(file, "Shelf_Cam.mp3"); 
      FileOutputStream fos = new FileOutputStream(outputFile); 

      InputStream is = c.getInputStream(); 

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      while ((len1 = is.read(buffer)) != -1) { 
       fos.write(buffer, 0, len1); 
      } 
      fos.close(); 
      is.close(); 


     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show(); 
     } 
    } 


call this in a Asynctask 




    public class downloadMusic extends AsyncTask<Integer, Integer, Integer> 
     { 


      @Override 
      protected Integer doInBackground(Integer... params) { 
       // TODO Auto-generated method stub 

       try { 

        DownLoadTheMusic(); 

       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       return null; 

      } 

      @Override 
      protected void onPostExecute(Integer result) { 
       // TODO Auto-generated method stub 
       super.onPostExecute(result); 
       Context context=shelf.this; 
       pd.dismiss(); 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "Shelf_Cam.mp3")), "application/vnd.android.package-archive"); 
       startActivity(intent); 

      } 

      @Override 
      protected void onPreExecute() { 
       // TODO Auto-generated method stub 
       super.onPreExecute(); 
       pd=ProgressDialog.show(shelf.this,"Updating","Please wait....."); 
      } 

     } 

執行此任務onbutton點擊使用從互聯網上這個,如果你正在下載的小音樂文件。

+0

即時通訊設法使用你的代碼並實現它,但仍然沒有成功zny其他想法? – MikeyBoy 2013-03-02 14:10:35