Android AsyncTask onProgressUpdate - 如何获取上传文件的进度百分比?

时间:2014-08-21 09:39:26

标签: android android-asynctask android-progressbar

我正在使用Async任务并使用onProgressUpdate上传图片。它运行正常,但我在获取正在上传的文件的百分比方面遇到了问题。如何才能知道当前上传文件的百分比。 以下是我使用的代码。

private class ImageUpload extends AsyncTask<String,Integer,String> {

        @Override
        protected void onProgressUpdate(Integer... progress) {
            // TODO Auto-generated method stub

            mProgressDialog.setProgress((int) (progress[0]));
        }

        @Override
        protected String doInBackground( String... params ) {


            String result = "";


            HttpClient httpClient = new DefaultHttpClient();


            HttpPost httpPostRequest = new HttpPost( "http://192.168.212.177:8089/services/imageUpload" );

            try {
                Date todaysdate = new Date();
                for ( String imageFile : params ) {
                    Bitmap bm = BitmapFactory.decodeFile(imageFile);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    bm.compress(Bitmap.CompressFormat.JPEG, 15, baos); //bm is the bitmap object   
                    byte[] b = baos.toByteArray(); 
                    String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

                    FileBody bin = new FileBody( file );

                    MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();

                    int strIndex = imageFile.lastIndexOf( "/" );
                    String imageName = imageFile.substring( strIndex + 1 , imageFile.length() );

                    multiPartEntityBuilder.addPart( "imageFile" , new StringBody(encodedImage , ContentType.TEXT_PLAIN ) );
                    multiPartEntityBuilder.addPart( "filename" , new StringBody( farmersEnteredNumber + "_" + datemill.getTime() , ContentType.TEXT_PLAIN ) );
                    multiPartEntityBuilder.addPart( "imageName" , new StringBody( imageName, ContentType.TEXT_PLAIN ) );


                    long total = 0;

                    long lenghtOfFile = multiPartEntityBuilder.build().getContentLength();
                    publishProgress((int)((total*100)/lenghtOfFile));
                    httpPostRequest.setEntity( multiPartEntityBuilder.build() );


                    HttpResponse httpResponse = null;
                    httpResponse = httpClient.execute( httpPostRequest );


                    InputStream inputStream = null;
                    inputStream = httpResponse.getEntity().getContent();

                    if ( inputStream != null )
                        result = convertInputStreamToString( inputStream );
                    else
                        result = "Did not work!";
                    return result;
                }
            } catch ( Exception e ) {

                return null;
            }

            return result;
        }

        @Override
        protected void onPreExecute() {

            // TODO Auto-generated method stub
            super.onPreExecute();
            FarmerDetails.this.runOnUiThread( new Runnable() {

                public void run() {

                    mProgressDialog = new ProgressDialog( FarmerDetails.this );
                    mProgressDialog.setTitle( "Image Uploading" );
                    mProgressDialog.setMessage( "Uploading Image Please Wait" );
                    mProgressDialog.setIndeterminate( false );
                    mProgressDialog.setMax(100);
                    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    mProgressDialog.setCancelable(true);
                    mProgressDialog.show();

                }
            } );



        }

        @Override
        protected void onPostExecute( String result ) {
            super.onPostExecute( result );
            mProgressDialog.dismiss();
            // TODO Auto-generated method stub


        }

    }

0 个答案:

没有答案