本文介绍了如何从服务器上通过URL下载的音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从服务器下载音频文件通过URL,并将其保存到SD卡。

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

我用下面的code:

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

}

以上code不下载音频文件。如果使用menifest文件PLZ任何权限告诉我..(我用互联网许可)请大家帮忙

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

感谢..

推荐答案

您例如未指定请求​​方法和一些MIME类型和材料。
在这里,你会发现MIME类型的列表 http://www.webmaster-toolkit.com/mime -types.shtml
找到相关的MIME类型给你,并将其添加到code以下指定的MIME类型。

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.

呵呵,并顺便说一句,下面是普通的Java code。你将不得不更换存储在SD卡的文件位。不要有一个仿真器或手机来测试部分的时刻另请参阅文档存储权限SD此处的

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

然后调用,

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

编辑:坏codeR我。裹在的BufferedInputStream该输入的InputStream。无需指定buffersizes等。
默认值是好的。

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

这篇关于如何从服务器上通过URL下载的音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:45
查看更多