如何设置进度条和进度通知栏对上传图片?(How to set Progress Bar and Pr

2019-10-23 13:19发布

在我的应用程序,我上传图片到web服务器,所以我想显示在通知抽屉中的进度条和通知进度条,我已经尝试了一些方法,但进度对话和通知栏显示不同的上传百分比,我不“知道,如何解决这一问题issue.Any帮助是appreciated.Can任何人的帮助?

进度条的方法。 在这种方法我打电话来调用通知抽屉。进度栏的方法,我在这里执行我的AsyncTask任务。

ProgressDialogueBox

  public void progressDialogBox(View view){
    progress=new ProgressDialog(this);
    progress.setMessage("Uploading Image");
    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progress.setIndeterminate(false);
    progress.setCancelable(false);
    progress.setCanceledOnTouchOutside(true);
    progress.setProgress(0);
    progress.show();
    final int totalProgressTime = 100;
    final Thread t = new Thread() {
        @Override
        public void run() {
            int jumpTime = 0;
            while(jumpTime < totalProgressTime) {
                try {

                    ProgressBarNotification();
                    new ImageUploadTask().execute();
                    sleep(2000);
                    jumpTime += 5;

                    progress.setProgress(jumpTime);
                }
                catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
}

进步通知方法

 public void ProgressBarNotification(){
    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mBuilder = new NotificationCompat.Builder(ImageUploadActivity.this);
    mBuilder.setContentTitle("Upload")
            .setContentText("Upload in progress")
            .setSmallIcon(R.drawable.ic_launcher);

   // mBuilder.setAutoCancel(true);
    Intent myIntent = new Intent(this, ImageUploadActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(ImageUploadActivity.this, 0,   myIntent, Intent.FILL_IN_ACTION);
    mBuilder.setContentIntent(pendingIntent);

}

图片上传活动 - 图片上传都是在这里完成

class ImageUploadTask extends AsyncTask<Void,Integer, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // Displays the progress bar for the first time.
        mBuilder.setProgress(100, 0, false);
        mNotifyManager.notify(id, mBuilder.build());
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        // Update progress
        mBuilder.setProgress(100, values[0], false);
        mNotifyManager.notify(id, mBuilder.build());
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(Void... unsued) {
      int i;

    for (i = 0; i <= 100; i += 5) {
            // Sets the progress indicator completion percentage
        publishProgress(Math.min(i, 100));


            try {



                Thread.sleep(2 * 1000);

                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                  HttpPost httpPost = new HttpPost("http://11.10.10.14/test/upload.php");

                MultipartEntity entity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                byte[] data = bos.toByteArray();


                entity.addPart("uploaded_file", new ByteArrayBody(data,
                        "myImage.jpg"));

                // String newFilename= filename.concat("file");
                // newFilename=filename+newFilename;

               /* entity.addPart("uploaded_file", new ByteArrayBody(data,
                        filename));*/
                Log.e(TAG, "Method invoked");
                httpPost.setEntity(entity);
                HttpResponse response = httpClient.execute(httpPost,
                        localContext);
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                response.getEntity().getContent(), "UTF-8"));

                StringBuilder builder = new StringBuilder();
                String aux = "";

                while ((aux = reader.readLine()) != null) {
                    builder.append(aux);
                    if (isCancelled()) break;
                }

                String sResponse = builder.toString();

                return sResponse;


            } catch (Exception e) {
             /*   if (dialog.isShowing())
                    dialog.dismiss();
                Toast.makeText(getApplicationContext(), "Exception Message 1", Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);
                return null;*/
            }}

       // }
        return null;
    }
    @Override
    protected void onPostExecute(String sResponse) {

     if (sResponse != null) {

         Toast.makeText(getApplicationContext(),
                 "Photo uploaded successfully",
                 Toast.LENGTH_SHORT).show();

     }
       // super.onPostExecute(result);
        mBuilder.setContentText("Upload complete");
        // Removes the progress bar
    //  mBuilder.setProgress(0, 0, false);
        //mNotifyManager.notify(1, mBuilder.build());
        mNotifyManager.cancel( id);
    }
}}

上传按钮

 upload = (Button) findViewById(R.id.upload);
    upload.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (bitmap == null) {
                Toast.makeText(getApplicationContext(),
                        "Please select image", Toast.LENGTH_SHORT).show();
            } else {
                progressDialogBox(v);//ProgressBar on the Activity

            }
        }
    });
}
文章来源: How to set Progress Bar and Progress Notification Bar for an Image Upload?