简体   繁体   English

成功上传图片后不显示Toast消息

[英]Does not show Toast message after image is uploaded successfully

My current code uploads image successfully but still it does not show Toast message which says Image uploaded successfully . 我当前的代码成功上传了图像,但仍然没有显示Toast消息,提示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 这是imageUpload方法

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;
}

you are trying to display Toast in non UI Thread so can't see Toast 您正在尝试在非UI线程中显示Toast,因此看不到Toast

Use Handler after completion of Task to show Toast.its better to use AsyncTask. Task完成后使用Handler来显示Toast.its更好地使用AsyncTask。

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

I would suggest you to implement AsyncTask which is known as Painless Threading in android. 我建议您实现AsyncTask ,这在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. 在您的情况下,您可以在doInBackground()方法内调用imageUpload(),从onPostExecute()方法显示Toast,而无需关心线程。

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. 按用户- 帕雷什Mayani萨米尔Mangroliya建议为的AsyncTask也更好地做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM