2014-01-16 2 views
-3

버튼을 클릭하면 내 서버에서 URL을 통해 오디오를 가져오고 싶습니다.Android 다운로드 서버에서 오디오

어떻게해야합니까?

+0

당신은 분명하지 질문입니다. 당신은 무엇을하길 원합니까 ? –

+0

고마워요. 내가 버튼을 클릭하면 url 양식을 통해 오디오 서버에 오디오를 보내고 싶습니다. – user3187340

답변

2

다운로드 파일의 경우 다음 코드를 사용할 수 있습니다.

private class DownloadFile extends AsyncTask<String, Integer, String>{ 
@Override 
protected String doInBackground(String... url) { 
    int count; 
    try { 
     URL url = new URL("url of your .mp3 file"); 
     URLConnection conexion = url.openConnection(); 
     conexion.connect(); 
     // this will be useful so that you can show a tipical 0-100% progress bar 
     int lenghtOfFile = conexion.getContentLength(); 

     // downlod the file 
     InputStream input = new BufferedInputStream(url.openStream()); 
     OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.mp3"); 

     byte data[] = new byte[1024]; 

     long total = 0; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      // publishing the progress.... 
      publishProgress((int)(total*100/lenghtOfFile)); 
      output.write(data, 0, count); 
     } 

     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (Exception e) {} 
    return null; 
} 

매니페스트 권한 :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 

은 또한 수도 다음 링크를 참조하면 도움 :

how can i download audio file from server by url

Android:How to download the File from the server and save it in specific folder in sdcard

+0

작동하지 않습니까?! – user3187340

관련 문제