Issue
I was looking for answer for hours but without result.
My app is using cameraCaptureSessions.setRepeatingRequest and saving/refreshing into TextureView privateTextureView.
I want to take a picture from TextureView -> apply filter using AsyncTask -> save it to the ImageView. But getting warning "unchecked call to execute(Params...) as a member of the raw type AsyncTask"
Application falls on button click(AsyncTask execute). Where am I wrong?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
privateTextureView = (TextureView)findViewById(R.id.textureView);
privateTextureView.setSurfaceTextureListener(textureListener);
myTask = new ApplyFilterTask();
privateBtnGetCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pictureBitmap = privateTextureView.getBitmap();
//Start MyTask
myTask.execute(pictureBitmap);
}
});
}
protected class ApplyFilterTask extends AsyncTask<Bitmap, Bitmap, Bitmap> {
@Override
protected void onPreExecute() {
int color = Color.parseColor("#ffa500");
privateImageView.setColorFilter(color);
}
@Override
protected Bitmap doInBackground(Bitmap... bitmaps) {
Bitmap bmp = bitmaps[0];
try {
ImageFilters filter = new ImageFilters();
bmp = filter.applyContrastEffect(bmp, 5);
publishProgress(bmp);
} catch (Exception e) {
//System.out.println("Error!!!! " + e);
}
return bmp;
}
@Override
protected void onProgressUpdate(Bitmap... bm) {
System.out.println("TestProgress");
privateImageView.setImageBitmap(bm[0]);
}
/*
protected void onPostExecute(Bitmap... bm) {
System.out.println("TestExecude");
privateImageView.setImageBitmap(bm[0]);
}*/
}
Solution
The application failure mentioned by you seems to be due to multiple usages of the same AsyncTask. An Asynctask can be used only once. As mentioned in the official documentation :
The task can be executed only once (an exception will be thrown if a second execution is attempted.)
Your code creates a single myTask object that is used every time in the OnClick event listener. Instantiate a new AsyncTask everytime to avoid the application failures, as shown below.
privateBtnGetCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pictureBitmap = privateTextureView.getBitmap();
AsyncTask<Bitmap, Bitmap, Bitmap> myTask = new ApplyFilterTask();
//Start MyTask
myTask.execute(pictureBitmap);
}
});
The warning mentioned by the IDE is however a separate issue that has to do with the declaration of myTask. You seem to have declared myTask as AsyncTask and not as AsyncTask<Bitmap, Bitmap, Bitmap> or ApplyFilterTask. Change the declaration and the warning would disappear.
Answered By - Pranjal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.