11

I have been trying to download videos from a url, I have implemented my downloading method in the doInBackground() of asynctask, but the doInBackground method is taking a lot of time to get called(5-10 mins), I am using another asyntask to download image in the activity from which I am directed to download video activity and its working fine. My onPreExecute method is being called on time, but after that doInBackground takes almost 5-7 minutes to start. I will be really grateful for any help provided. Here is mycode

btnDownloadLQ.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) 
            {
                try
                {
                    new DownloadVideoTask().execute(videoURL);

                }
                catch(Exception e)
                {
                    Log.e("Vidit_TAG","I got an error",e);
                }
            }
        });

private class DownloadVideoTask extends AsyncTask<String, String, String> 
    {

        @SuppressWarnings("deprecation")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(DIALOG_DOWNLOAD_PROGRESS);
        }

        protected String doInBackground(String... urls) 
        {
            int i=0;
            try
            {
                URL url = new URL (urls[0]);
                InputStream input = url.openStream();

                try {
                    //The sdcard directory e.g. '/sdcard' can be used directly, or 
                    //more safely abstracted with getExternalStorageDirectory()
                    String root = Environment.getExternalStorageDirectory().toString();
                    File storagePath = new File(root + "/vidit");    
                    storagePath.mkdirs();
                    OutputStream output = new FileOutputStream (new File(storagePath,title+".mp4"));
                    try 
                    {
                        byte[] buffer = new byte[1024];
                        int bytesRead = 0;
                        while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) 
                        {
                            output.write(buffer, 0, bytesRead);
                        }
                    }
                    catch(Exception e)
                    {   
                        Log.e("Vidit_TAG","I got an error",e);
                    }
                    finally 
                    {
                        output.close();
                    }
                }
                catch(Exception e)
                {
                    Log.e("Vidit_TAG","I got an error",e);
                }
                finally 
                {
                    input.close();
                    //tvTitle.setText("Completed");
                }

            }
            catch(Exception e)
            {
                Log.e("Vidit_TAG","I got an error",e);
            }

            return null;
        }


        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(String unused) 
        {
            dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
            alertbox(title);
        }
    }
Y0gesh Gupta
  • 2,184
  • 5
  • 40
  • 56
  • 1
    Could you post your code? doInBackground is called as soon as onPreExecute is finished (and it exists), so there must be something wrong in how you're calling it or in your onPreExecute. – Steven Schoen Jan 13 '13 at 22:53
  • probably he is using post delayed, its impossible run after 10mins – deadfish Jan 13 '13 at 23:13
  • @D_Steve595 My onPreExecute method is being called on time which I found out while debugging, but after that doInBackground takes almost 5-7 minutes to start. – Y0gesh Gupta Jan 13 '13 at 23:18
  • @Lumma No post delayed is being used. – Y0gesh Gupta Jan 13 '13 at 23:19
  • Please make sure to provide code as reference, so it wont be a guess game – N Jay Jan 13 '13 at 23:19
  • Try putting a Log event at the very first lines of your onPreExecute and doInBackground, and also at the very last line of your onPreExecute. Then watch the log to see what it's getting stuck on. – Steven Schoen Jan 13 '13 at 23:47
  • 3
    Bear in mind that `AsyncTask` invocations may be serialized depending on what your project settings are and the Android version you are running on: http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html – CommonsWare Jan 13 '13 at 23:57
  • I was able to find the solution to the problem, as I felt the asynchtask started in the previous activity was delaying the call of doInBackground(), I went through another question of similar type in SO, and the answer to the post did it for me. http://stackoverflow.com/a/11977186/1897838 Use of executeOnExecutor() is making the threads run in parallel. – Y0gesh Gupta Jan 14 '13 at 00:02

3 Answers3

16

make sure no other asyncTasks are running , by cancelling them if needed.

on most android versions , asyncTask runs on a single background thread , and should only run small tasks .

in case the task might take too long (or there are multiple tasks) , consider cancelling them or use an alternative approach (like using executeOnExecutor as described on the API ) .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    yup did the same used executeOnExecutor.Thanks – Y0gesh Gupta Jan 14 '13 at 09:54
  • do note that using executeOnExecutor still gives you a lot of responsibility , so don't create too many tasks (they have a limit of how many tasks to have at a time) . plus the more threads there are , the slower the app will become . – android developer Jan 14 '13 at 09:57
5

Late Answer but surely helps

If you are using min API level >=11 try this

 //new YourAsyncTask().execute(); -- replace this with following line
 new YourAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); //use this
Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
0

I'm facing the same issue in spite of it didnot happen every time.

You could use the tradtional Thread for an alternative and handle the UI changes yourself