【问题标题】:Does not show Toast message after image is uploaded successfully图片上传成功后不显示 Toast 消息
【发布时间】:2012-06-23 10:16:21
【问题描述】:

我当前的代码成功上传了图片,但仍然没有显示 Image uploaded successfully 的 Toast 消息。图片上传成功后如何显示 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;
}

【问题讨论】:

  • Toast 中尝试使用Activityname.this 而不是getApplicationContext()
  • 使用 MyActivity.this 后仍然没有显示 Toast 消息
  • 尝试添加一些日志以查看是否正在输入 Toast 行。 if(response != null) { Log.d("TAG", "response not null");如果不是,那么问题不在于 Toast。
  • @NunoGonçalves 在添加 Log 后,如您所述,它在 logcat 中显示 response not null

标签: java android


【解决方案1】:

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

在Task完成后使用Handler来展示Toast。最好使用AsyncTask.

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

【讨论】:

  • @SamirMangroliya 可能有些不明白 UI 线程和工作线程之间的区别。
  • +1,您的要求正确。因为,必须使用处理程序(或)AsyncTask 进行上传或下载操作,仅检测进程完成事件!
  • @SamirMangroliya 是的,你说得对,我写了那个对你投了反对票的人,他可能不理解 UI 胎面和非 UI 之间的区别。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    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 MayaniSamir Mangroliya 建议 AsyncTask 也更好地做到这一点。

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2015-12-05
      • 1970-01-01
      • 2015-12-07
      • 2011-12-09
      相关资源
      最近更新 更多