Y0g*_*pta 11 android android-asynctask
我一直在尝试从网址下载视频,我已经在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);
}
}
Run Code Online (Sandbox Code Playgroud)
迟到的回答但肯定有帮助
如果您使用的最低 API 级别 >=11 试试这个
//new YourAsyncTask().execute(); -- replace this with following line
new YourAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); //use this
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5464 次 |
| 最近记录: |