Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Asynctask's doInBackground method is called after a long delay

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);
        }
    }
like image 939
Y0gesh Gupta Avatar asked Jan 13 '13 22:01

Y0gesh Gupta


1 Answers

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
like image 55
Ranjithkumar Avatar answered Jan 04 '23 09:01

Ranjithkumar