我需要知道为更新进度条 android 上传了多少字节 [英] I need know how much bytes has been uploaded for update a progress bar android

查看:14
本文介绍了我需要知道为更新进度条 android 上传了多少字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发用于将视频上传到 Apache/PHP 服务器的应用程序.这一刻我已经可以上传视频了.我需要在上传文件时显示进度条.我有下一个使用 AsyncTask 和 HTTP 4.1.1 库来模拟 FORM 的代码.

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

我需要知道在哪里可以获取已上传的字节数.File.length 是总大小.

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

推荐答案

在这里查看我的答案,我想它可以回答您的问题:但是将图片的文件路径更新为您要上传的视频

Check my answer here, I guess it answers your question: But update the file path of the image to your to be uploaded video

https://stackoverflow.com/questions/15572747/progressbar-in-asynctask-is-not-showing-on-upload

这篇关于我需要知道为更新进度条 android 上传了多少字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆