Toast fails to show when within a AsyncTask
-
05-02-2021 - |
Question
I have a simple application which sends an image (Base64 encoded) to a server, the server gets this data fine because the PHP script sends me an email with the Base64 Data attached. However, after the task gets completed the toast never shows. How do I take the Toast get shown after the data gets posted?
I think the issue is within the context.
UPDATE
I have updated the link, because i have since moved the upload logic into a different .java file.
Solution
Your sample look OK. If Activity, to which mContext variable belongs is currently active, it should show. Not in other case.
try this modification:
new UploadImage(ImageUploadActivity.this).execute(sentImage);
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
Android toast.makeText context error
EDIT: WRONG TYPE DECLARATION OF AsyncTask
your AsyncTask
declaration looks like class UploadImage extends AsyncTask<String, Void, String>
This means:
- is type of params to
doInBackground(String... arg)
- is type of progress
- is type of result from
doInBackground
toonPostExecute
So change your onPostExecute
declaration to this:
protected void onPostExecute(String result)
or change return type of doInBackground
to <Bitmap>
and change class declaration to: class UploadImage extends AsyncTask<String, Void, Bitmap>
http://developer.android.com/reference/android/os/AsyncTask.html