Question

I have to get the time of download in my android application, that's why I need to determine the time of beginning and end of dowload to determine the period od download. I added these line in the beginning and the end of doInBackground:

Log.v("Download_INFO","i begin downloading at : "+" "+dt.getHours()+"h "+dt.getMinutes()+"min "
                        +dt.getSeconds()+"sec");

Log.v("Download_INFO","i complete downloading at : "+" "+dt.getHours()+"h "+dt.getMinutes()
                        +"min "+dt.getSeconds()+"sec");

But the surprise is that i had the some time. I can't understand the reason

this is my doInBackground method :

protected String doInBackground(String... aurl) {
        int count;

        try {
            File vSDCard = null;
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                vSDCard = Environment.getExternalStorageDirectory();
                File vFile = new File(vSDCard.getParent() + "/" + vSDCard.getName() + "/"
                        + "downloadTest.jpg");
                URL url = new URL(aurl[0]);
                URLConnection conexion = url.openConnection();
                conexion.connect();
                int lenghtOfFile = conexion.getContentLength();
                Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream(vFile);
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress("" + (int) ((total * 100) / lenghtOfFile));
                    output.write(data, 0, count);
                }

                Log.v("Download_INFO","i complete downloading at : "+" "+dt.getHours()+"h "+dt.getMinutes()
                        +"min "+dt.getSeconds()+"sec");
                output.flush();
                output.close();
                input.close();
                System.out.println(currentDateTimeString);
            }

        } catch (Exception e) {
            Log.d("ImageManager", "Error: " + e);
        }

        return null;
    }

what's the problem please !

Was it helpful?

Solution

It looks like you're using the same date object for both log statements. Add this before your "download complete" log statement:

dt = new Date();

By the way, all those methods (getSeconds, getHours, etc) of Date are depricated. You can get the timing simply by doing a System.currentTimeMillis() at the beginning of the download and again at the end and subtracting. This is the total duration in milliseconds which you can convert into hours/min/seconds if you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top