user2432
user2432

Reputation: 1

how can i download audio file from server by url

how can i download audio file from server by url and save it to sdcard.

i am using the code below:

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);
    }

}

the above code is not downloading audio file. if use any permission in menifest file plz tell me.. (i have used internet permission) please help

thanks..

Upvotes: 0

Views: 10663

Answers (2)

n4rzul
n4rzul

Reputation: 4078

Your example does not specify a request method and some mimetypes and stuff.
Here you will find a list of mimetypes http://www.webmaster-toolkit.com/mime-types.shtml
Find the mimetypes relevant to you and add it to the mimetypes specified below in the code.

Oh and btw, the below is normal Java code. You'll have to replace the bit that stores the file on the sdcard. dont have an emulator or phone to test that part at the moment Also see the docs for storage permissions on sd here: http://developer.android.com/reference/android/Manifest.permission_group.html#STORAGE

  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();
  }

Then call,

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

EDIT: Bad coder me. Wrap that input InputStream in a BufferedInputStream. No need to specify buffersizes ect.
Defaults are good.

Upvotes: 2

Anuj Tenani
Anuj Tenani

Reputation: 2094

you must also add

android.permission.WRITE_EXTERNAL_STORAGE

permission if you wish to write data to sd card.

also post your logcat output , if you are getting any IOExceptions.

Upvotes: 2

Related Questions