簡體   English   中英

成功上傳圖片后不顯示Toast消息

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

我當前的代碼成功上傳了圖像,但仍然沒有顯示Toast消息,提示Image uploaded successfully 成功上傳圖片后如何顯示吐司消息?

這是我的代碼

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

您正在嘗試在非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.添加imageUpload code in DoinBackground(...) method and return boolean to onPostExxecute(),and You can display Toast in OnPostExecute Method.

我建議您實現AsyncTask ,這在android中被稱為無痛線程

在您的情況下,您可以在doInBackground()方法內調用imageUpload(),從onPostExecute()方法顯示Toast,而無需關心線程。

我不知道這是對還是錯。 您可以嘗試這樣-

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

            }
        }
    });
}

我認為這會起作用。 按用戶- 帕雷什Mayani薩米爾Mangroliya建議為的AsyncTask也更好地做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM