Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need know how much bytes has been uploaded for update a progress bar android

I am developing an app for upload video to a Apache/PHP Server. In this moment I already can upload videos. I need show a progress bar while the file is being uploaded. I have the next code using AsyncTask and HTTP 4.1.1 Libraries for emulate the FORM.

class uploadVideo extends AsyncTask<Void,Void,String>{

    @Override
    protected String doInBackground(Void... params) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.youtouch.cl/videoloader/index.php");           
        try {
            // Add your data
            File input=new File(fileName);              

            MultipartEntity multi=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);                

            multi.addPart("video", new FileBody(input));                

            httppost.setEntity(multi);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);               

            HttpEntity entity = response.getEntity();

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            entity.getContent(), "UTF-8"));
            String sResponse = reader.readLine();
            return sResponse;

        } catch (ClientProtocolException e) {
            Log.v("Uri Galeria", e.toString());
            e.printStackTrace();                

        } catch (IOException e) {
            Log.v("Uri Galeria", e.toString());
            e.printStackTrace();                
        }
        return "error";
    }

    @Override
    protected void onProgressUpdate(Void... unsued) {
                //Here I do should update the progress bar
    }

    @Override
    protected void onPostExecute(String sResponse) {
        try {
            if (pd.isShowing())
                pd.dismiss();

            if (sResponse != null) {
                JSONObject JResponse = new JSONObject(sResponse);
                int success = JResponse.getInt("SUCCESS");
                String message = JResponse.getString("MESSAGE");
                if (success == 0) {
                    Toast.makeText(getApplicationContext(), message,
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Video uploaded successfully",
                            Toast.LENGTH_SHORT).show();

                }
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }

I need know where I can get how much bytes has been uploaded. File.length is the total size.

like image 393
ClarkXP Avatar asked Feb 24 '23 10:02

ClarkXP


2 Answers

Have you tried extending FileBody? Presumably the POST will either call getInputStream() or writeTo() in order to actually send the file data to the server. You could extend either of these (including the InputStream returned by getInputStream()) and keep track of how much data has been sent.

like image 108
cyngus Avatar answered Apr 09 '23 18:04

cyngus


thank to cyngus's idea I have resolved this issue. I have added the next code for tracking the uploaded bytes:

Listener on upload button:

    btnSubir.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //pd = ProgressDialog.show(VideoAndroidActivity.this, "", "Subiendo Video", true, false);

            pd = new ProgressDialog(VideoAndroidActivity.this);
            pd.setMessage("Uploading Video");
            pd.setIndeterminate(false);
            pd.setMax(100);
            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pd.show();
            //Thread thread=new Thread(new threadUploadVideo());
            //thread.start();
            new UploadVideo().execute();
        }
    });

Asynctask for run the upload:

class UploadVideo extends AsyncTask<Void,Integer,String> {
    private FileBody fb;

    @Override
    protected String doInBackground(Void... params) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.youtouch.cl/videoloader/index.php");   
        int count;
        try {
            // Add your data
            File input=new File(fileName);

            // I created a Filebody Object
            fb=new FileBody(input);
            MultipartEntity multi=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            multi.addPart("video",fb);          

            httppost.setEntity(multi);              
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            //get the InputStream
            InputStream is=fb.getInputStream();

            //create a buffer
            byte data[] = new byte[1024];//1024

            //this var updates the progress bar
            long total=0;
            while((count=is.read(data))!=-1){
                total+=count;
                publishProgress((int)(total*100/input.length()));
            }
            is.close();             
            HttpEntity entity = response.getEntity();

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            entity.getContent(), "UTF-8"));
            String sResponse = reader.readLine();
            return sResponse;

        } catch (ClientProtocolException e) {
            Log.v("Uri Galeria", e.toString());
            e.printStackTrace();                

        } catch (IOException e) {
            Log.v("Uri Galeria", e.toString());
            e.printStackTrace();                
        }
        return "error";
    }

    @Override
    protected void onProgressUpdate(Integer... unsued) {        
        pd.setProgress(unsued[0]);
    }

    @Override
    protected void onPostExecute(String sResponse) {
        try {
            if (pd.isShowing())
                pd.dismiss();

            if (sResponse != null) {
                    Toast.makeText(getApplicationContext(),sResponse,Toast.LENGTH_SHORT).show();
                    Log.i("Splash", sResponse);                 
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }


}

The progress bar load is bit slow (in starting seems be freeze and then load of 1 to 100 very fast), but works.

Sorry, my english is regular :(.

like image 36
ClarkXP Avatar answered Apr 09 '23 19:04

ClarkXP