English 中文(简体)
我需要知道,有多少 by被上载,以更新进展巴和roid。
原标题:I need know how much bytes has been uploaded for update a progress bar android

我正在开发一个将录像带上载到阿帕奇/PHP服务器的备份。 目前,我已经能够上载录像。 在档案上卸载时,我需要显示一个进展障碍。 我有下一部法典,使用AsyncTask和HTTP 4.1.1 图书馆复制南太平洋大学。

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

我需要知道,我能从哪里得到多少用药。 文件。 长度是总规模。

最佳回答

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

>

问题回答

你们是否尝试延长档案。 简言之,《名录》将打电话getInputStream()或writeTo(>,以便实际向服务器发送档案数据。 您可以延长其中任一部分(包括按<代码>归还的InputStream(),并跟踪发送的数据数量。

我已经解决这个问题。 我增加了下一部关于跟踪用箱上载荷的代码:

载重吨清单:

    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,用于上载:

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


}

进度货车的装载速度不大(从冻结开始,然后装满1至100只快),但效果良好。

职业介绍会,我的办公室是经常的:

我使用的是扩大org.apache. http://www.entity.ByteArrayEntity,并超越以下文字的字面功能,而其产出则通过书写(To)(英文),因此,你可以按字母顺序计算目前的产出:

@Override
public void writeTo(final OutputStream outstream) throws IOException 
{
    if (outstream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }

    InputStream instream = new ByteArrayInputStream(this.content);

    try {
        byte[] tmp = new byte[512];
        int total = (int) this.content.length;
        int progress = 0;
        int increment = 0;
        int l;
        int percent;

        // read file and write to http output stream
        while ((l = instream.read(tmp)) != -1) {
            // check progress
            progress = progress + l;
            percent = Math.round(((float) progress / (float) total) * 100);

            // if percent exceeds increment update status notification
            // and adjust increment
            if (percent > increment) {
                increment += 10;
                // update percentage here !!
            }

            // write to output stream
            outstream.write(tmp, 0, l);
        }

        // flush output stream
        outstream.flush();
    } finally {
        // close input stream
        instream.close();
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签