Reputation: 1527
Does there is a way to run my AsyncTask after it finish ?
My app is to capture an image and put it on imagView then execute an AsyncTask on it. I have another button to take another photo then put it put cannot execute an AysncTask.
I know form developer.Android that AsyncTask can not execute only except one time.
This is my code.
package com.ocr.id;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.ocr.id.ip.AndroidImage;
import com.ocr.id.ip.Binarization;
import com.ocr.id.ip.Crop;
import com.ocr.id.ip.Segement;
public class PreviewActivity extends Activity {
private ImageView previewIV;
private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private String path;
private boolean crop = true;
private boolean resample = true;
Crop cropID;
Binarization binary;
Segement seg;
ProgressDialog previewPD;
OnClickListener processOnClickListener = new OnClickListener() {
public void onClick(View v) {
segementTask task = new segementTask();
task.execute(path);
}
};
private OnClickListener backOnClickListener = new OnClickListener() {
public void onClick(View v) {
startActivityForResult(
new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(
MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(path))),
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
previewIV.setImageBitmap(AndroidImage
.decodeSampledBitmapFromSDcard(path, 1000, 1000));
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.preview);
path = getIntent().getExtras().getString("path");
previewIV = (ImageView) findViewById(R.id.previewPicID);
Button process = (Button) findViewById(R.id.processID);
process.setOnClickListener(processOnClickListener);
Button back = (Button) findViewById(R.id.back);
back.setOnClickListener(backOnClickListener);
previewIV.setImageBitmap(AndroidImage.decodeSampledBitmapFromSDcard(
path, 1000, 1000));
}
class segementTask extends AsyncTask<String, Integer, Void> {
@Override
protected void onPreExecute() {
previewPD = ProgressDialog.show(PreviewActivity.this, "Id-OCR",
"Processing...");
previewPD.setCancelable(false);
super.onPreExecute();
}
@Override
protected Void doInBackground(String... params) {
try {
seg = new Segement(PreviewActivity.this, params[0]);
seg.segmentNumbers();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
previewPD.dismiss();
previewPD = null;
Toast.makeText(PreviewActivity.this, "Processing done",
Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
}
}
Upvotes: 0
Views: 734
Reputation: 132982
try as:
@Override
protected void onPostExecute(Void result) {
previewPD.dismiss();
previewPD = null;
if (task.getStatus() == AsyncTask.Status.FINISHED)
tTaskFinished();
Toast.makeText(PreviewActivity.this, "Processing done",
Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
protected void tTaskFinished( ){
task = new MyAsyncTask();
task.execute(path);
}
Upvotes: 0
Reputation: 2573
If you want to keep the same worker thread, and start processing the second image as soon as the first one is finished, a better solution would probably be using a Handler. It allows you to post jobs so that they will be carried out in sequence.
Upvotes: 1
Reputation: 2029
protected void onPostExecute(Void result) {
previewPD.dismiss();
previewPD = null;
Toast.makeText(PreviewActivity.this, "Processing done",
Toast.LENGTH_LONG).show();
super.onPostExecute(result);'
if(setAcondition) {
new segementTask().execute(whateverPath);
}
}
Let me know if this is what you're looking for.
Upvotes: 0
Reputation: 25793
You can't run the same instance twice. But you can call new segmentTask().execute()
as many times as you want.
Upvotes: 2
Reputation: 5326
Set a count
int mNumTaskFinished = 0;
Create a protected method:
protected method didTaskFinished( AsyncTask task ){
if(mNumTaskFinished == 0){
task.execute(path);
}
}
And inside your AsyncTask you can call:
@Override
protected void onPostExecute(Void result) {
previewPD.dismiss();
previewPD = null;
Toast.makeText(PreviewActivity.this, "Processing done",
Toast.LENGTH_LONG).show();
didTaskFinished(this);
super.onPostExecute(result);
}
Upvotes: 0