我一直试图从一个url下载视频,我在asynctask的doinbackground()中实现了我的下载方法,但是doinbackground方法要花很多时间才能被调用(5-10分钟),我正在使用另一个asyntask下载指向的活动中的图像。下载视频活动及其工作正常。我的onpreexecute方法正在被准时调用,但是在那之后doinbackground几乎需要5-7分钟才能开始。我将非常感谢你的帮助。
这是麦可德

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);
        }
    }

最佳答案

确保没有其他异步任务正在运行,如果需要,可以取消它们。
在大多数android版本中,asynctask运行在单个后台线程上,并且应该只运行小任务。
如果任务可能需要太长时间(或有多个任务),请考虑取消它们或使用其他方法(如使用executeonexecutor,如on the API所述)。

07-28 01:13
查看更多