0

My current code uploads image successfully but still it does not show Toast message which says Image uploaded successfully. How can I show toast message after image is uploaded successfully?

Here is my code

 public void onClick(View v) {
                        dialog = ProgressDialog.show(Camera.this, "", "Uploading file...", true);
                        new Thread() {
                            public void run() {
                                try {
                                    //Toast.makeText(getBaseContext(), "File is uploading...", Toast.LENGTH_LONG).show();
                                    if(imageUpload(globalUID, largeImagePath)) {
                                         Toast.makeText(getApplicationContext(), "Image uploaded", Toast.LENGTH_LONG).show();
                                     } else {
                                         Toast.makeText(getApplicationContext(), "Error uploading image", Toast.LENGTH_LONG).show();
                                     }
                                } catch (Exception e) {

                                }

                            }
                        }.start();

here is imageUpload method

public boolean imageUpload(String uid, String imagepath) {
    boolean success = false;
    //Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    Bitmap bitmapOrg = BitmapFactory.decodeFile(imagepath);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

    byte [] ba = bao.toByteArray();
    String ba1=Base64.encodeToString(ba, 0);

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("image",ba1));
    nameValuePairs.add(new BasicNameValuePair("uid", uid));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.example.info/androidfileupload/index.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        if(response != null) {
            Toast.makeText(getApplicationContext(), "File uploaded successfully", Toast.LENGTH_LONG).show();
            success = true;
        }
        is = entity.getContent();
    } catch(Exception e) {
        Log.e("log_tag", "Error in http connection "+e.toString());
    }
    dialog.dismiss();
    return success;
}
Om3ga
  • 30,465
  • 43
  • 141
  • 221

3 Answers3

5

you are trying to display Toast in non UI Thread so can't see Toast

Use Handler after completion of Task to show Toast.its better to use AsyncTask.

Using AsyncTask,add your imageUpload code in DoinBackground(...) method and return boolean to onPostExxecute(),and You can display Toast in OnPostExecute Method.

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • @SamirMangroliya maybe some nope that don't understand to difference between UI thread and worker thread. – Simon Dorociak Jun 23 '12 at 10:30
  • +1,your asking correct.because,must use handler (or) AsyncTask for upload or download operations for the process completed event only detecting! – Dinesh Jun 23 '12 at 10:34
  • 1
    @SamirMangroliya yes you right, i wrote that man that downvoted you maybe don't understand difference between UI tread and non UI. – Simon Dorociak Jun 23 '12 at 10:35
2

I would suggest you to implement AsyncTask which is known as Painless Threading in android.

And in your case, you can call imageUpload() inside the doInBackground() method, display Toast from onPostExecute() method, no need to take care about Threads.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
1

I don't know this is right or wrong. You can try like this -

public void onClick(View v)
{
    ProgressDialog dialog = ProgressDialog.show(DummyUsageActivity.this, "", "Uploading file...", true);
    this.runOnUiThread(new Runnable() {
        public void run() {
            try {
                //Toast.makeText(getBaseContext(), "File is uploading...", Toast.LENGTH_LONG).show();
                if(imageUpload(globalUID, largeImagePath)) {
                     Toast.makeText(DummyUsageActivity.this, "Image uploaded", Toast.LENGTH_LONG).show();
                 } else {
                     Toast.makeText(DummyUsageActivity.this, "Error uploading image", Toast.LENGTH_LONG).show();
                 }
            } catch (Exception e) {

            }
        }
    });
}

I think this will work. As per users - Paresh Mayani & Samir Mangroliya suggestion for AsyncTask also better to do this.

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173