AsyncTask. Как поймать onPostExecute в вызывающем классе
Всем привет. Имею класс вызывающий загрузку изображений с сервера в память устройства.
Вызывающий класс вызывает класс загрузчика в цикле.
Хочу повесить дополнительное условие для каждой итерации если в ответ onPostExecute -> true, то есть нужно обрабатывать как то ошибки для до следующего вызова.
Вот пример кода.
Вызывающий класс :
`for(int i = 0 ; i < 6; i++)
switch (i){
case 0:
new Download(getActivity(),"https://mydomain.com/","0002-2021-08-10.jpg","mBipmapsImage1");
break;
case 1:
new Download(getActivity(),"https://mydomain.com/","0027-2021-08-11.jpg","mBipmapsImage2");
break;
case 2:
new Download(getActivity(),"https://mydomain.com/","0104-2021-08-10.jpg","mBipmapsImage3");
break;
case 3:
new Download(getActivity(),"https://mydomain.com/","0356-2021-08-12.jpg","mBipmapsImage4");
break;
case 4:
new Download(getActivity(),"https://mydomain.com/","0409-2021-08-04.jpg","mBipmapsImage5");
break;
}`
Класс с AsyncTask :
public Download (Context context, String mUrl, String mImageName,String mfile_name){
this._context = context;
sUrl = mUrl;
sImageName = mImageName;
file_name = mfile_name;
requestStoragePermission();
}
public class ShareTask extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
URL myFileUrl;
Bitmap bmImg = null;
File file;
public ShareTask(Context context) {
this.context = context;
}
//------------------------------------------Диалог --------------------------------------//
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(_context);
pDialog.setMessage(_context.getString(R.string.loading_msg));
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
//----------------------------------------- HttpURLConnection --------------------------------------//
@Override
protected String doInBackground(String... args) {
try {
myFileUrl = new URL(args[0]);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
try {
String path = myFileUrl.getPath();
String idStr = path.substring(path.lastIndexOf('/') + 1);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath() + "/" + _context.getResources().getString(R.string.app_name) + "/");
dir.mkdirs();
String fileName = idStr;
file = new File(dir, file_name);
FileOutputStream fos = new FileOutputStream(file);
bmImg.compress(Bitmap.CompressFormat.PNG, 99, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pDialog != null)
{
pDialog.dismiss();
}
}