1. Download xml file
The solution that suited me best was largely built on this stackoverflow entry. I put it in an AsyncTask.
Remember manifext update:
<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>
In main activity.
URL url = null; try { url = new URL("https://www.ecb.int/stats/exchange/eurofxref/html/index.en.html"); } catch (MalformedURLException e) { e.printStackTrace(); } new AsyncNetworkConnectionInstant(this).execute(url);
Create class AsyncNetworkConnectionInstant.
public class AsyncNetworkConnectionInstant extends AsyncTask<URL, Integer, Long> { @Override protected Long doInBackground(URL... urls) { long downloadedSize = 0; try { URL url = urls[0]; // create the new connection HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // set up some things on the connection urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); // and connect! urlConnection.connect(); File dir = new File("/sdcard/" + "rm1/"); if(dir.exists()==false) { Log.e("AsNCI", "Creating dir"); dir.mkdirs(); } File file = new File(dir, "ecb1.xml"); FileOutputStream fileOutput = new FileOutputStream(file); InputStream inputStream = urlConnection.getInputStream(); int totalSize = urlConnection.getContentLength(); downloadedSize = 0; byte[] buffer = new byte[1024]; int bufferLength = 0; // used to store a temporary size of the buffer while ((bufferLength = inputStream.read(buffer)) > 0) { fileOutput.write(buffer, 0, bufferLength); downloadedSize += bufferLength; int progress = (int) (downloadedSize * 100 / totalSize); } fileOutput.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return downloadedSize; } protected void onPostExecute(Long result) { //Do something with file }
2. Write to sd card
Found that either of these methods are possible.
A. Simplest is to use the Android Virtual Device Manager. Edit your AVD and insert a value into SD card size.
B. In a command prompt go to androidsdk\tools and enter
mksdcard 64M MyNewSdCard
Then either edit your AVD again and enter the SD card path or continue from the command prompt by entering
emulator -avd -sdcard MyNewSdCard
Whichever method used, once the emulator is running it can be explored from Eclipse by selecting Window, Open perspective, other. DDNS, File Explorer (top of panel on right). The sdcard can be found under the mnt folder.