0

我当前的代码成功上传了图像,但仍然没有显示 Toast 消息Image uploaded successfully。图片上传成功后如何显示 toast 消息?

这是我的代码

 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();

这是 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;
}
4

3 回答 3

5

您正在尝试在非 UI 线程中显示 Toast,因此看不到 Toast

Task 完成后使用 Handler显示 Toast.its 更好地使用AsyncTask。

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

于 2012-06-23T10:17:40.240 回答
2

我建议你实现AsyncTask ,它在 android中被称为Painless Threading 。

在您的情况下,您可以在方法内部调用 imageUpload() doInBackground(),从方法中显示 Toast onPostExecute(),无需关心线程。

于 2012-06-23T10:28:24.353 回答
1

我不知道这是对还是错。你可以这样试试——

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) {

            }
        }
    });
}

我认为这会奏效。根据用户 - Paresh Mayani和Samir MangroliyaAsyncTask的建议也更好地做到这一点。

于 2012-06-23T10:35:09.747 回答