Próbowałem pobierać filmy z adresu URL, zaimplementowałem moją metodę pobierania w funkcji asynctask doInBackground(), ale metoda doInBackground zajmuje dużo czasu nazywam się (5-10 minut), używam innego asyntask do pobrania obrazu w działaniu, z którego jestem skierowany, aby pobrać aktywność wideo i działa poprawnie. Moja metoda onPreExecute jest wywoływana na czas, ale po tym doInBackground zajmuje prawie 5-7 minut, aby rozpocząć. Będę naprawdę wdzięczny za każdą udzieloną pomoc. Oto mycodeAndroid: Metoda doInBackground Asynctask jest wywoływana po długim opóźnieniu
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);
}
}
Czy możesz napisać swój kod? doInBackground jest wywoływane, gdy tylko onPreExecute jest zakończony (i istnieje), więc musi coś być nie tak z tym, jak go wywołujesz lub w swoim onPreExecute. –
prawdopodobnie używa on opóźnienia, jego niemożliwe uruchomienie po 10 minutach – deadfish
@ D_Steve595 Moja metoda onPreExecute jest wywoływana na czas, który dowiedziałem się podczas debugowania, ale po tym doInBackground trwa prawie 5-7 minut, aby rozpocząć. –