How to save file from website to sdcard

眉间皱痕 提交于 2019-11-26 08:06:02

问题


Does anyone know how to save a file from a webserver(local host) to the sdcard through wifi?

I am doing xml parsing to my application and for that I have to download an xml file from localhost to the sdcard and then tag the parsing. I am stuck with downloading an xml file to the sd card. Please guide me on how to do this..


回答1:


You can use this method to download a file from the internet to your SD card:

public void DownloadFromUrl(String DownloadUrl, String fileName) {

   try {
           File root = android.os.Environment.getExternalStorageDirectory();               

           File dir = new File (root.getAbsolutePath() + "/xmls");
           if(dir.exists()==false) {
                dir.mkdirs();
           }

           URL url = new URL(DownloadUrl); //you can write here any link
           File file = new File(dir, fileName);

           long startTime = System.currentTimeMillis();
           Log.d("DownloadManager", "download begining");
           Log.d("DownloadManager", "download url:" + url);
           Log.d("DownloadManager", "downloaded file name:" + fileName);

           /* Open a connection to that URL. */
           URLConnection ucon = url.openConnection();

           /*
            * Define InputStreams to read from the URLConnection.
            */
           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);

           /*
            * Read bytes to the Buffer until there is nothing more to read(-1).
            */
           ByteArrayBuffer baf = new ByteArrayBuffer(5000);
           int current = 0;
           while ((current = bis.read()) != -1) {
              baf.append((byte) current);
           }


           /* Convert the Bytes read to a String. */
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(baf.toByteArray());
           fos.flush();
           fos.close();
           Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");

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

}

You need to add the following permissions to your AndroidManifest.xml:

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



回答2:


The Sourav's answer is OK, but for large file sizes you should implement it on a new thread; because while downloading, the main thread is busy for downloading the file. If the waiting time be more that expected the system will generate "Not responding" error.

In order to do that you can use "TaskAsync" or "IntentService"




回答3:


I prefer to build soap server and make a call from app to server and by that receiving XML. Or maybe you could just make an URL which generates XML and that just parse URL directly.

Try to read more on this LINK

Hopefully I have answer the question. Otherwise would be pleased to help, but I need more detailed functionality description.



来源:https://stackoverflow.com/questions/5472226/how-to-save-file-from-website-to-sdcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!