How to download file on android emulator?
by using web services we are able to download file from cloud to buffer storage. We need to now access this downloaded file from buffer memory and move it to SD card. I am using Eclipse and Android pl开发者_开发技巧ug in.
Can someone share any code ?
My code snippet is below.
String filename = "Test.zip";
URL url = new URL(FromUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
FileOutputStream fileOutput = openFileOutput(filename, Context.MODE_WORLD_READABLE);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
//close the output stream when done
fileOutput.close();
return filename;
Just saving to the sdcard?
fileoutput = new FileOutputStream("/sdcard/"+filename);
That's quick and dirty, but works on every android so far found. If you want to be a bit neater...
File ofile = new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS),filename);
FileOutputStream f = new FileOutputStream(ofile);
精彩评论