Android: Asynctask doInBackground method called after a long delay

I'm trying to download a video from a URL, I applied my upload method to doInBackground () asynctask, but the doInBackground method takes a long time to get a call (5-10 minutes), I use another asyntask to load the image into the action from which I I'm going to download video activity and its work. My onPreExecute method is called on time, but after that doInBackground takes almost 5-7 minutes to start. I will be very grateful for any help provided. Here is my code

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 eg '/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); } } 
+8
android android-asynctask
source share
3 answers

make sure other asyncTasks are not running by canceling them if necessary.

on most versions of Android, asyncTask runs on a single background thread and should only perform small tasks.

if a task can take too much time (or there are several tasks), consider canceling or using an alternative approach (for example, using executeOnExecutor, as described in the API )).

+12
source share

I ran into the same problem, even though this did not happen every time.

You can use the traditional thread as an alternative and edit the user interface yourself

0
source share

Late answer, but certainly helps

If you use a minimum API level> = 11, try this

  //new YourAsyncTask().execute(); -- replace this with following line new YourAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); //use this 
0
source share

All Articles