У меня есть загрузчик изображений. Если подключение к Интернету прекращается, отображается диалоговое окно с предупреждением, в котором пользователю предлагается повторить попытку или отменить загрузку. Проблема в том, что иногда при повторной попытке после второй загрузки загружаются два изображения вместо одного. В чем может быть проблема?
Вот мой код:
@Override
protected Void doInBackground(Void... params) {
Log.i("ImageUpload", "Uploading..");
setBitmap();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(url);
httpPostRequest.setHeader("Cookie", this.cookie);
MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder
.create();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
this.image.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
multiPartEntityBuilder.addBinaryBody("picture", byteArray,
ContentType.create("image/png"), "image.png");
httpPostRequest.setEntity(multiPartEntityBuilder.build());
try {
HttpResponse httpResponse = null;
Log.i("ImageUpload", "====================================================\n"
+ "httpClient.execute(httpPostRequest)");
httpResponse = httpClient.execute(httpPostRequest);
Log.i("ImageUpload", "====================================================");
Log.i("ImageUpload", "httpPost executed");
InputStream inputStream = null;
inputStream = httpResponse.getEntity().getContent();
String result;
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
} else {
result = "Unable to send signiture.";
}
Log.i("Upload result", result);
} catch (IOException e) {
photoSent = false;
httpPostRequest.abort();
this.cancel(true);
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
this.mainActivity.hideProgressBar();
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(
mainActivity);
myAlertDialog
.setTitle("Error why uploading the picture.");
myAlertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
myAlertDialog.setPositiveButton("Retry",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/*execute the whole AsyncTask again*/
}
});
myAlertDialog.show();
super.onCancelled();
}