I want to make a download manager for android I want to know how can I add
pause and resume functionality I am using HttpUrlConnection class of android
for getting the data.I also want to know how can I access the InputStream which I get from HttpUrlConnection.getInputStream() which get Interrupted because of the Internet connection(say user turn off internet connection).
any sample code will be helpful
Downloader example here
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
}else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
Credits: https://stackoverflow.com/users/705297/pomatu
Related
For example I am downloading a file from server, in between the connectivity is lost at that point in time my download was 30%, after some time I got a connection .Now I want to start a downloading from 30%, not from 0%.How to achieve this asynctask android.
If any alternative is there please, let me know.???
You need to first figure out how many bytes you have actually downloaded. I suggest saving your file with an different name when it's being downloaded so you can can easily see if you have an unfinished download.
Check the status of you file first to see how much you have downloaded.
private long isIncomplete(){
File from = new File(dir,fileName+"-incomplete");
if(from.exists()){
Log.d("status","download is incomplete, filesize:" + from.length());
return from.length();
}
return 0;
}
Then when creating your http request you can tell the server from what point to serve you the file so that you can resume download.
long downloaded = isIncomplete();
urlConnection.setRequestProperty("Range", "bytes="+(downloaded)+"-");
See this class that I wrote a few years back for a complete implementation.
Update: I suggest you do not use the Shared Preferences for this. SSOT states that you get the info from only one source not more hence reading the progress from the downloaded file.
You can store the destination file path in sharedpreferences and you can do the following code.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();//Opening the url
File file=new File(DESTINATION_PATH);
if(file.exists()){ //check if file exists
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
pBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
pBar.setProgress(downloaded);
}
I am using the following code for downloading file from server:
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.connect();
StorageTools.maintainMemoryEmptyThreshold(25);
if (tempFolderFile.createNewFile()) {
tempFolderFile.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(tempFolderFile);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[8096];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
LOGGER.debug("Progress: downloadedSize: " + downloadedSize + "totalSize: " + totalSize);
}
fileOutput.close();
if (downloadedSize == totalSize) {
LOGGER.info("Downloaded at " + tempFolderFile.getAbsolutePath());
StorageTools.moveFile(tempFolder, targetFolder, fileName);
return true;
}
I trigger the download using a new thread into a tempfolder, move it into required folder upon download completion. How to maintain a cap on the number of active download threads and add remaining downloads to a queue ?
Also, following are my concerns in this regard:
Is there any way in which I can incorporate a threadpool executor for queuing downloads using above function ?
Does the buffersize impact the download speed ?
Please check https://developer.android.com/reference/android/app/DownloadManager.html
buffersize will not impact much. I think it should be in safe limit.
Any one help me..
My problem is that when i am downloading file sometimes server is stooped & download is done only 50% after server start I want download from 51%. because 50% download is completed before server stop.
My code is below..but its not working.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
}else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
Thanks.
I want to be able to open a http connection to a given file in Android and start downloading it.
I also have to be able to pause the download at some point and resume it later.
How is this achieved in Android? I don't want to start the download all over again.
Such a downloader has been posted here:
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(ISSUE_DOWNLOAD_STATUS.intValue()==ECMConstant.ECM_DOWNLOADING){
File file=new File(DESTINATION_PATH);
if(file.exists()){
downloaded = (int) file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
}
}else{
connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
progressBar.setMax(connection.getContentLength());
in = new BufferedInputStream(connection.getInputStream());
fos=(downloaded==0)? new FileOutputStream(DESTINATION_PATH): new FileOutputStream(DESTINATION_PATH,true);
bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
progressBar.setProgress(downloaded);
}
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.