How to download a file from the web using Android 1.6 - android

I am trying to download a zipped file from the web on the first instance of the application; can someone point me to a tutorial for this (I don't see good documentation on developer.android)? I understand how to check if it's the initial start or not, and also how to use java.util.zip once I get the file, but it's the in-between where I'm lost.

I think your question is about how to download the file. So, to download a file, use code similar to the following:
URL u = new URL("http://www.example.com/downloadfile.zip");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream fNew = new FileOutputStream(new File(root,"downloadfile.zip"));
InputStream inStream = c.getInputStream();
byte[] buffer = new byte[1024];
int inlen = 0;
while ( (inlen = inStream.read(buffer)) > 0 ) {
fNew.write(buffer,0, inlen);
}
fNew.close();
inStream.close();
Of course, wrap with appropriate error checking

Related

How to download audio file in Android

I have a published Android application that has an HTTP audio download process.
This processed worked fine until today.
whats wrong with my code?
final URL downloadFileUrl = new URL(mPerformanceSong.getPreviewUrl());
final HttpURLConnection httpURLConnection = (HttpURLConnection) downloadFileUrl.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.connect();
mTrackDownloadFile = new File(RecordPerformance.this.getCacheDir(), "mediafile");
mTrackDownloadFile.createNewFile();
final FileOutputStream fileOutputStream = new FileOutputStream(mTrackDownloadFile);
final byte buffer[] = new byte[16 * 1024];
final InputStream inputStream = httpURLConnection.getInputStream();
int len1 = 0;
while ((len1 = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len1);
}
fileOutputStream.flush();
fileOutputStream.close();
The content of the downloaded file appears to be gzip.
does this mean i need to wrap my inputStream in GZIPInputStream?
an example download URL is
http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI
I'm actually surprised it's downloading anything - are you sure it is?
The http url that you've posted:
http://www.amazon.com/gp/dmusic/get_sample_url.html?ASIN=B008TMSNMI
Is currently redirecting to an https cloudfront address:
https://d28julafmv4ekl.cloudfront.net/...
HttpUrlConnection will not follow redirects across schemes (ie from http to https).
So if you change your *.amazon.com URLs to https, perhaps it would fix your issue...

downloading terminates when App goes to background

I am working on an App, that uses phonegap.
When the App is started for the first time, I am downloading the data from the server.
That downloading process is being executed in AsyncTask, and in onPostExecute() of that, I am loading the url.
The problem being arose is that, when my download process is getting executed, and the App somehow goes to the background, the downloading gets terminated, unfortunately, which is absolutely not required.
To overcome this trouble; I guess, using service is one of the good options. But, in that case, another question arises, that is, how can I possibly, load the url inside service.
Otherwise, what else can be done?
Please, suggest me the possible ways to get rid off this trouble, I am currently facing.
Use the similar method for downloading the data. This may be helpful.
public void downloadUsingGET(String apkurl, String fileName) {
try {
String PATH = Environment.getExternalStorageDirectory()+"/Android/data/"+getPackageName()+"/files/";
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e) {
}
}
If you use a service, you can pass strings trough an intent(the intent you started the service).

Zip file download in android

I have an app to download zip file content from drop box (which is publicly shared path). i wrote download code using HttpURLConnection but its not working as intended and instead is downloading a small portion (after download zip file showing 31 kb but its original size is 3mb). i am attching my code. please help me to solve this.
URL url = new URL("drop box public share url");
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setAllowUserInteraction(false);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setConnectTimeout(5 * 1000);
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"/download/sample.zip");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
onProgressUpdate(downloadedSize, totalSize);
}
//close the output stream when done
fileOutput.close();
inputStream.close();
It seems that the method call:
setDoOuput(true);
makes the request a POST (see
What exactly does URLConnection.setDoOutput() affect?)
Removing it seems to fix the issue.
Try to use plain URLConnection not HttpURLConnection. And see the output.

Android: How do I get the file date from a URL for an image I want to download?

I want to query the server to see if the date is different than the one I have on the phone
that I previously downloaded. I want to keep from un-necessarily downloading the image again if I already have it. Looking for a way to get remote URL file date AND time without downloading the image again. Code Below that fetches all.
File root = Environment.getExternalStorageDirectory();
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
InputStream in = c.getInputStream();
Drawable d = Drawable.createFromStream(in, "src name");
printi("Content length",c.getContentLength());
File root = Environment.getExternalStorageDirectory();
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("HEAD");
c.connect();
InputStream in = c.getInputStream();
long dt=c.getLastModified(); //File Modified Date!!!
printtime("File Time ",dt);
c.disconnect(); //close connection ????

Some download troubles in Android

I use below code to download file:
URL u = new URL(one.getSrcPath());
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.setReadTimeout(10000);
c.connect();
int lenghtOfFile = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/" + SavePath, FileName);
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
int finishbyte = 0;
long total = 0;
while((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
f.write(buffer, 0, len1);
finishbyte++;
}
f.close();
I have two problems:
First, why my download task download fail very high frequency?
Second, if I want my download task resume from break point.
I have get the finishbyte.
How can I modify?
finishbyte both does not represent any information (except the number of calls to the read method, but certainly not the size of the downloaded file), and is not relevant, since you have written to a file and can use the File.length() method to know how much you got so far.
To resume a download:
Open your file, check the size, request a range using the http header that is:
Range: <file.length()>-
(example, if you have downloaded 234 bytes:
Range: 234-
If the response code from the server is 206 Partial Content, you can append to your file, if it is 200, you have to overwrite your file (content have changed or Range is not supported)
To start downloading a file starting with finishbyte position, you will have to use the Range HTTP header. As for the failed downloads problem, it's probably a network issue or phone sleep issue, in which case you should check out the wifi lock

Categories

Resources