2011-07-28 5 views
2

私は現在、様々なmp3リンクを持つwebviewを持っています。ユーザーがこれらのリンクの1つを押すと、alertDialogが表示され、ファイルを聞くかダウンロードするかを選択できます。私のダウンロード部分は、(asynctaskを介して)意図して動作していますが、私はSDCARD上のmp3ファイルが呼び出される名前を指定する場所を現在設定しています。トラックの名前がmp3ファイルの名前になるようにしたいと思います。どのように私はそれを行うことができるかについての任意のアイデア?ありがとう。ここでAndroid - URL命名の問題で、MP3をSDCARDにダウンロード

は、私のコードの一部です:

//so you can click on links in app and not open the actual browser. will stay in app 
    private class HelloWebViewClient extends WebViewClient{  
     @Override 
     public boolean shouldOverrideUrlLoading(final WebView view, final String url){ 
      view.loadUrl(url); 
      view.getSettings().getAllowFileAccess(); 
      view.getSettings().setJavaScriptEnabled(true); 
      //load the dropbox files so people can listen to the track 
      if(url.endsWith(".mp3")){ 
       progressWebView.dismiss(); 
       progressWebView.cancel(); 
       blogDialog.setButton("Listen", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(Intent.ACTION_VIEW); 
         intent.setDataAndType(Uri.parse(url), "audio/*"); 
         view.getContext().startActivity(intent); 

        } 
       }); 
       blogDialog.setButton2("Download", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         sdrUrl = url.toString(); 
         new DownloadFile().execute(); 


        } 

       }); 
       blogDialog.show(); 

      }else{ 
       return super.shouldOverrideUrlLoading(view, url); 
      } 
      return true; 
     } 
    } 

    //to handle the back button 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event){ 
     if((keyCode == KeyEvent.KEYCODE_BACK) && sdrWebView.canGoBack()){ 
      sdrWebView.goBack(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

    public void onPause(){ 
     super.onPause(); 
    } 

    /*create the pop up menu so you can reload*/ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu){ 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item){ 
     switch (item.getItemId()){ 
     case R.id.refreshsetting: sdrWebView.loadUrl("http://www.stopdroprave.com"); 
     break; 
     } 
     return true; 
    } 

    private class DownloadFile extends AsyncTask<String, Integer, String>{ 
     @Override 
     protected String doInBackground(String... url) { 
      try { 
       URL url2 = new URL(sdrUrl); 
       HttpURLConnection c = (HttpURLConnection) url2.openConnection(); 
       c.setRequestMethod("GET"); 
       c.setDoOutput(true); 
       c.connect(); 

       int lengthOfFile = c.getContentLength(); 

       String PATH = Environment.getExternalStorageDirectory() 
         + "/download/"; 
       Log.v("", "PATH: " + PATH); 
       File file = new File(PATH); 
       file.mkdirs(); 

       String fileName = "testSDRtrack.mp3"; 

       File outputFile = new File(file, fileName); 
       FileOutputStream fos = new FileOutputStream(outputFile); 

       InputStream is = c.getInputStream(); 

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

       }catch (IOException e) { 
         e.printStackTrace(); 
       } 


      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 

      super.onProgressUpdate(values); 
     } 



} 
} 

答えて

関連する問題