2015-01-13 115 views
2

我在本网站下载更多的解释,从网址下载MP3或图片,我遵循更多的方法,并尝试写我的方法,但是当我运行应用程序停止。Android:从网址下载

我做的方法来查询下载点击
也把权限INTERNET & WRITE_EXTERNAL_STORAGE

把问题依然

这种方法是下载

public static void downloadMain(){ 
    File fileToSave = null; 
    String scrPath ="http://***"; 
    BufferedInputStream bis; 
    BufferedOutputStream bos; 

    fileToSave = new File(Environment.getExternalStorageDirectory().getPath() +"/"+ "A"+"/"); 
    if (!fileToSave.exists()) 
     fileToSave.mkdirs(); 
    fileToSave = new File(Environment.getExternalStorageDirectory().getPath() +"/"+ "A" +"/" + "h"+"/"); 
    if (!fileToSave.exists()) 
     fileToSave.mkdirs(); 
    File file = new File (fileToSave,"***.mp3"); 
    try{ 
     URL url = new URL(scrPath+"***.mp3"); 
     URLConnection ucon = url.openConnection(); 
     ucon.connect(); 
     bis=new BufferedInputStream(ucon.getInputStream()); 

     bos = new BufferedOutputStream(new FileOutputStream(file)); 
     bis=new BufferedInputStream(url.openStream()); 
     byte[] data = new byte[1024]; 
     int a =0; 
     while(true){ 
      int k = bis.read(data); 
      if(k==-1){ 
       bis.close(); 
       bos.flush(); 
       bos.close(); 

       break; 
      } 
      bos.write(data, 0, k); 
      a+=k; 
     } 
    }catch(IOException e){} 
} 
+0

请编辑您的问题,包括堆栈跟踪从logcat。 – Simon

回答

0

我有三个主要的困惑时关于您的程序:

  1. 你是否在asynctask中运行以下代码? (这必须运行asincronusly否则它会阻止)
  2. 为什么它无限循环?
  3. 您无法打开带有“*”的命名URL或文件在其内部

编辑:

必须运行下载方法asincronusly否则它不会工作, 与文件系统和网络互动不能在主线程

EDIT2来完成:

的AsyncTask应该是这样的

private class DownloadTask extends AsyncTask<String, Integer, String> { 

private Context context; 

    public DownloadTask(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected String doInBackground(String... sUrl) { 
     InputStream input = null; 
     OutputStream output = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      // expect HTTP 200 OK, so we don't mistakenly save error report 
      // instead of the file 
      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 

      // download the file 
      input = connection.getInputStream(); 
      output = new FileOutputStream("/sdcard/file_name.extension");//put here your path and your mkdirs 

      byte data[] = new byte[4096]; 
      long total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 

       total += count; 
       output.write(data, 0, count); 
      } 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (output != null) 
        output.close(); 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 
@Override 
    protected void onPostExecute(String result) { 
     if (result != null) 
      Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show(); 
     else 
      Toast.makeText(context,"File downloaded", Toast.LENGTH_SHORT).show(); 
    } 
} 

你shoould这样称呼它

DownloadTask downloadTask = new DownloadTask(YourActivity.this); 
downloadTask.execute("the url to the file you want to download"); 

你也可以看看这个answer

+0

1-我尝试使用三种genric的asyncTask,但它也没有运行.2 - 从停止输入流中读取循环,直到等于-1。 3-我使用了正确的网址,但听到我放* – user4450367

+0

@ user4450367你可以把你的asynctask的代码? –

+0

我尝试使用此synctask,但更改storge路径和url也用onClickListener查询由null consturctor http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress -in-a-progressdialog – user4450367