2011-09-08 5 views
0

url로 서버로부터 오디오 파일을 다운로드 해, sdcard에 보존 할 수있는 방법.url로 서버로부터 오디오 파일을 어떻게 다운로드 할 수 있습니까

public void uploadPithyFromServer(String imageURL, String fileName) { 

    try { 
     URL url = new URL(GlobalConfig.AppUrl + imageURL); 
     File file = new File(fileName); 

     Log.d("ImageManager", "download begining"); 
     Log.d("ImageManager", "download url:" + url); 
     Log.d("ImageManager", "downloaded file name:" + fileName); 
     /* Open a connection to that URL. */ 
     URLConnection con = url.openConnection(); 

     InputStream is = con.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is, 1024 * 50); 
     FileOutputStream fos = new FileOutputStream("/sdcard/" + file); 
     byte[] buffer = new byte[1024 * 50]; 

     int current = 0; 
     while ((current = bis.read(buffer)) != -1) { 
      fos.write(buffer, 0, current); 
     } 

     fos.flush(); 
     fos.close(); 
     bis.close(); 

    } catch (IOException e) { 
     Log.d("ImageManager", "Error: " + e); 
    } 

} 

위의 코드는 오디오 파일을 다운로드되지 않습니다

나는 아래의 코드를 사용하고 있습니다. menifest 파일에 모든 권한을 사용 경우 PLZ

+0

오디오 파일이 다운로드되지 않는다는 것을 어떻게 알 수 있습니까? – slayton

답변

2

당신은 또한

android.permission.WRITE_EXTERNAL_STORAGE

를 추가해야합니다 .. 이

감사를 도와주세요 (내가 인터넷 권한을 사용했다) .. 말해

SD 카드에 데이터를 쓰고 싶다면 허락을 받으십시오.

또한 IOException이 발생하는 경우 logcat 출력을 게시하십시오.

2

귀하의 예에서는 요청 방법과 일부 MIME 유형 및 내용을 지정하지 않습니다.
여기에 mimetypes 목록이 있습니다 http://www.webmaster-toolkit.com/mime-types.shtml
당신과 관련된 mimetyp을 찾아 아래 코드에 지정된 mimetypes에 추가하십시오.

오 및 btw, 아래는 일반적인 자바 코드입니다. sdcard에 파일을 저장하는 비트를 대체해야합니다. 순간 에서 그 부분을 테스트하는 에뮬레이터 또는 전화를 해달라고 또한 여기에 SD에 저장 권한에 대한 문서를 참조하십시오

public static void downloadFile(String hostUrl, String filename) 
    { 
    try {  
    File file = new File(filename); 
    URL server = new URL(hostUrl + file.getName()); 


    HttpURLConnection connection = (HttpURLConnection)server.openConnection(); 
    connection.setRequestMethod("GET"); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 
    connection.addRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*"); 
    connection.addRequestProperty("Accept-Language", "en-us,zh-cn;q=0.5"); 
    connection.addRequestProperty("Accept-Encoding", "gzip, deflate"); 

    connection.connect(); 
    InputStream is = connection.getInputStream(); 
    OutputStream os = new FileOutputStream("c:/temp/" + file.getName()); 

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

    os.close(); 

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

http://developer.android.com/reference/android/Manifest.permission_group.html#STORAGE 그런 다음 전화

downloadFile("http://localhost/images/bullets/", "bullet_green.gif"); 

편집 : 나 코더 나쁜. InputStream를 BufferedInputStream에 랩핑하십시오. 버퍼링을 지정하지 않아도됩니다.
기본값은 양호합니다.

관련 문제