Android 2.2 - Downloading only every 2 files - other one fails - android

I'm a newbie at Android, and developing my first app.
I'm using the following code to download 10 images from my website
private class DownloadImages extends AsyncTask<Integer, Integer, Integer> {
#Override
protected Integer doInBackground(Integer... pic_ids){
int count = pic_ids.length;
for (int i = 0; i < count; i++) {
// download the image from website ...
String url = "http://www.mysite.com/pic.php?pid=" + pic_ids[i];
DownloadFromUrl(url, pic_ids[i] + ".jpg");
publishProgress((int) (i + 1));
}
return count;
}
}
public void DownloadFromUrl(String imageURL, String fileName){
try {
URL url = new URL(imageURL);
File file = new File(getFilesDir() + "/" + fileName);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8192);
ByteArrayBuffer baf = new ByteArrayBuffer(128);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
} catch (Exception e) {
Log.d("ImageManager", "Error: " + e);
}
}
The "DownloadImages" is passed an array of 10 ID's. All are handled (I have Log.d statements in between to check), however, only 1 out of 2 actually gets downloaded. Not in a random way: the first one is downloaded, the second one not, the third one is downloade, the fourth not etc. That means: The files are all written to the phone/emulator, but those that "fail" are empty.
When I start it again with only the 5 remaining images (those that were not downloaded the first time), the same thing happens (1st one downloaded, 2nd one not, ...)
I don't see an error in the logs.
Adding a check for baf.length shows that this is 0 for those files that fail.
Any idea ?

This is long past due but I was having the same issue a few months ago. The strange thing was it only happened while connecting with SSL.
I eventually had to implement a checker to see if it truly downloaded and if not tried again (terminated after the second attempt at each file).

Related

add checking update function in android app

I want to add checking update function in android app
I know this question is asked already and post a lib on Github
How to allow users to check for the latest app version from inside the app?
but my cant i post on the Google Play Store due to some problem of copyright
So i cant use the method
what should i do?
Your code to check update once in a day. only works when you open app once in a day. in onCreate() of your launcher activity.
SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences
(Dashboard.this);
long lastUpdateTime = mSettings.getLong("lastUpdateTime", 0);
/* Should Activity Check for Updates Now? */
if ((lastUpdateTime + (24 * 60 * 60 * 1000)) < System.currentTimeMillis()) {
/* Save current timestamp for next Check*/
SharedPreferences.Editor editor = mSettings.edit();
lastUpdateTime = System.currentTimeMillis();
editor.putLong("lastUpdateTime", lastUpdateTime);
editor.commit();
/* Start Update by using asynctask that run in backGround eg. http://portal.aksuniversity.com:8089/utility/apkversion*/
String URL = "your app url in my case ;
AsyncRequest asyncRequestTime = new AsyncRequest(Dashboard.this, "GET", null,
null, 3);
asyncRequestTime.execute(URL);
}
the response you will get is json object. get jsonObject key of version code and match it with your app version code
int versionCode = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0).versionCode;
if (versionCode < jsonObject.getString(""VersionCode))
//if greater show dialog for app update and download apk with new url of your app which will give you apk
for download you can use
private String callApiDownload(String address) {
try {
URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (Cookie.getCookie() != null)
urlConnection.addRequestProperty("Cookie", Cookie.getCookie());
if (urlConnection.getResponseCode() == 200) {
File file = new File(Environment.getExternalStorageDirectory() + "/" + folderName);
boolean fileExist = true;
if (!file.exists()) {
fileExist = file.mkdir();
}
if (fileExist) {
String FileName = Environment.getExternalStorageDirectory() + "/" + folderName + "/"
+ fileName;
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(FileName);
int bytesRead;
byte[] buffer = new byte[4096];
int total = 0;
int contentLength = urlConnection.getContentLength();
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
publishProgress((int) ((total * 100) / contentLength));
}
}
outputStream.flush();
outputStream.close();
inputStream.close();
return fileName;
}
} else {
InputStream inputStream = new BufferedInputStream(urlConnection.getErrorStream());
return fileName = Connection.convertInputStreamToString(inputStream);
}
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return fileName;
}
on download complete you can use intent to open apk for installation which will prompmt user for new update apk installation.
The first code will call the api once a day when user open the app. This api will return the json object of your app version code and version name. You need to manually maintain it when you create new apk for user. Parse the json object. And get the version code. In your app check if api version code is greater than your app version code. And if it is than fire a new api which is the last code i have given that download the your apk from url(in server you have to place apk for specified urlby calling which your apk will downloaded) which is last code you need to run in backthread asynctak. I will create the source code more proper and notifie you.

InputStream Exception while downloading a file

I am trying to download a PPT file from a server.
it's in Bytes.
but while debugging I noticed that the input stream throws an exception of FileNotFound while running.. the file does exist on the server, here's my code, any help would be greatly appreciated.
public class DownloadFileAsync extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("Authorization", "Basic " + SharedPref.getAuthPrefValue());
connection.addRequestProperty("Device", BaseApplication.getCurrentDevice().getDevice().toString());
connection.addRequestProperty("DeviceId", BaseApplication.getCurrentDevice().getDeviceId());
connection.connect();
int lengthOfFile = connection.getContentLength();
Log.d("ANDRO_ASYNC", "Length of file: " + lengthOfFile);
InputStream input = new BufferedInputStream(url.openStream());
File sdcardDest = new File(Environment.getExternalStorageDirectory(), "Availo");
String finalDest = sdcardDest + File.separator + "Check1" + "." + "PPT";
OutputStream output = new FileOutputStream(finalDest);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lengthOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I am using Charles on Mac (which is similar to fiddler on windows.) to see what I send and receive from the server,
The server doesn't return any error, though it shows download steps for 6-7 sec, downloading around 400 bytes and then it stops.
The Exception is thrown from the input stream line.
Thanks!
I suggest you take a look at the DownloadManager system service. it's designed specifically for what you are trying to do:
(from the documentation)
The download manager is a system service that handles long-running
HTTP downloads. Clients may request that a URI be downloaded to a
particular destination file. The download manager will conduct the
download in the background, taking care of HTTP interactions and
retrying downloads after failures or across connectivity changes and
system reboots
While I do agree with Muzikant regarding the download manager,
FileNotFoundException is usually thrown when... the file is not found on the local device...
You need to do the following to make sure it doesnt happen
File dest = new File(finalDest);
try{
File parentDest = dest.getParentFile();
if(!parentDest.exists()){
parentDest.mkdirs(); //make all the directory structures needed
}
if(!dest.exists()){
dest.createNewFile();
}
OutputStream output = new FileOutputStream(dest);
//now you can use your file dest
//write data to it...
}catch (Exception e){
}

downloading a file between 1MB and 2MB hangs

working on my first real android app and one part of it is a download manager.
it must download video files that the app uses to teach people Gaelic.
it works fine on all but 3 files, it downloads bigger files and smaller files. but will not download colours.mp4 1.86MB, weekdays 1.53 MB or numbers 1.99 MB all the files between 1MB and 2MB.
it will just stop at the end of the download loop no errors for a few minutes just looking like its waiting to continue downloading then it will give an error "unexpected end of stream"
can anyone suggest what the problem could be please?
I have crated the same app for the iPhone and that app has not got the same problem with these files.
this is the download loop called from inside an asynctask.
protected void download(String where, String file){
try {
//makes output file
OutputStream output = new FileOutputStream(getFilesDir()
.getAbsolutePath() + "/vidos/" +file);
int count=0;
//gets url to download from
URL url = new URL(where);
URLConnection conection = url.openConnection();
conection.connect();
//gets the length of the file to work out percent downloaded
int lengthOfFile = conection.getContentLength();
InputStream input = null;
Log.v("downloading", String.valueOf(showprg));
input = new BufferedInputStream(url.openStream());
byte data[] = new byte[lengthOfFile];
long total = 0;
Log.v("downloading", "size: " + String.valueOf(downloading));
while ((count = input.read(data)) > 0 && downloading) {
total += count;
publishProgress(String
.valueOf((int) ((total * 100) / lengthOfFile)));
output.write(data, 0, count);
//this is where it brakes
};
Log.v("publishProgress", "done");
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
}
I changed my URLConnection to a HttpURLConnection and removed the BufferedInputStream and now it downloads without problem.
thanks to Harshit Rathi for his link to Cristian's post

Manually Handle MP3 Download in WebView

I have this snippet of code in which I want to handle the downlaod of the file being clicked:
else if (url.startsWith("http://rapahh.com/songs2/Music%20Promotion/Download/")) {
}
return false;
Although I have no idea how to handle downloads in Android, so does anyone have a snippet of code I can use to download the file in the background to a folder.. the download folder is fine. Thanks.
What version of android are you building for?
Starting with API lvl 9 there is the DownloadManager that can handle this for you. If at all possible you should use the DownloadManager, because it will automatically handle network interuptions and resume the downloads for you.
If you are aiming for lower API lvl than that you'll have to make the download code yourself. You'll have an inputStream coming from your web source and an outputStream going to your local file and you will loop through the inputStream writing chunks until there are none left.
Something like this:
try {
URL url = new URL(URL); //URL of the video
//Set our file to the correct path and name.
File file = new File(PATH + fileName);
//keep the start time so we can display how long it took to the Log.
long startTime = System.currentTimeMillis();
Log.d(myTag, "download begining");
//Log.d(myTag, "download url:" + url);
Log.d(myTag, "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = ucon.getContentLength();
Log.i(myTag, "Opened Connection");
/************************************************
* Define InputStreams to read from the URLConnection.
************************************************/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Log.i(myTag, "Got InputStream and BufferedInputStream");
/************************************************
* Define OutputStreams to write to our file.
************************************************/
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
Log.i(myTag, "Got FileOutputStream and BufferedOutputStream");
/************************************************
* Start reading the and writing our file.
************************************************/
byte data[] = new byte[1024];
long total = 0;
int count;
//loop and read the current chunk
while ((count = bis.read(data)) != -1) {
//Post our progress update back to the UI thread
postProgress((int)(total*100/lenghtOfFile));
//write this chunk
total += count;
bos.write(data, 0, count);
}
//Have to call flush or the video file can get corrupted and won't play correctly.
bos.flush();
bos.close();
Log.d(myTag, "download ready in "
+ ((System.currentTimeMillis() - startTime))
+ " milisec");
} catch (IOException e) {
Log.d(myTag, "Error: " + e);
}
You'll need to implement the postProgress(int progress) method to do whatever is appropriate for your application to inform the user of what percentage complete the download is.
Edit:
you can comment out the logs to get it to work. I leave them on while I am debugging though to make the process easier. Log statements such as Log.i(String tag, String text)
are similar to System.out.println(String txt) The difference is that these statements are printed into the log file ( which you can see in the DDMS perspective in eclipse) And they have an additional parameter called "tag" you can pass it whatever string you like and this string will show up along side of your text in the log file. You can also filter the log output basted on these tags in the DDMS perspective. It is common practice to declare your tag as a static String so that you can just use that reference to it for all of your log statements and you are guaranteed to always have the same tag. So if you add something like this to your class it should fix your error:
final static String myTag = "NameOfYourActivity";

Android mp4 video from online getting corrupted when downloaded

I am working on an application that plays a video file on a loop. Up until now I was just mounting the device and copying the video file onto the SD card and then using the file path to start it up on my VideoView. I am trying to implement a way that I can remotely update what video it plays so I have moved to storing my video online. Inside the app I check for a local copy and download if it doesn't exist, or if there is a newer one. I have tested it on two different video files both .mp4s. After downloading one of them plays the first time but upon trying to start again for the loop it tells me video cannot be played. The other won't even play the first time, it just gives me the dialog that says the video cannot be played. Both of these files work correctly with my app if I copy them onto the SD card via the USB cable. They work if I exit my app and manually download them with something else(dropbox) but not if I download them from within my app. Here is the code I am using to download the file:
public static void DownloadFromUrl(String fileName) { //this is the downloader method
try {
URL url = new URL("http://dl.dropbox.com/u/myfile.mp4");
File file = new File(PATH + fileName);
long startTime = System.currentTimeMillis();
Log.d(myTag, "download begining");
Log.d(myTag, "download url:" + url);
Log.d(myTag, "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
Log.i(myTag, "Opened Connection");
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Log.i(myTag, "Got InputStream and BufferedInputStream");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
Log.i(myTag, "Got FileOutputStream and BufferedOutputStream");
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
int current = 0;
Log.i(myTag, "About to write");
while ((current = bis.read()) != -1) {
bos.write(current);
}
fos.close();
Log.d(myTag, "download ready in"
+ ((System.currentTimeMillis() - startTime))
+ " sec");
} catch (IOException e) {
Log.d(myTag, "Error: " + e);
}
}
I know the dropbox url in this snippet is not correct I changed it only for this post, in my app the url is pointing to a file correctly. And the variable PATH thats used when creating the File is set in my code outside of this snippet.
Is there something about this code snippet that could be corrupting my mp4 files?
That method was corrupting the file somehow, I am still not quite sure how but I changed part of it and now it is fixed.
I am now using this:
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = bis.read(data)) != -1) {
total += count;
fos.write(data, 0, count);
}
fos.flush();
fos.close();
instead of the old while loop and it works correctly.

Categories

Resources