【问题标题】:showing progress bar while uploading a video上传视频时显示进度条
【发布时间】:2012-04-16 19:51:03
【问题描述】:

我想在将视频从我的应用程序上传到 php 服务器时显示进度条。下面将文件正确上传到服务器。但我不知道如何显示进度条。如果有人知道请帮助我。

这是我的代码:

 private void doFileUpload(){
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 8*1024*1024;
            Cursor c = (MainscreenActivity.JEEMAHWDroidDB).query((MainscreenActivity.TABLE_Name), new String[] {
                     (MainscreenActivity.COL_HwdXml)}, null, null, null, null,
                              null);
             if(c.getCount()!=0){
             c.moveToLast();
             for(int i=c.getCount()-1; i>=0; i--) {
                  value=c.getString(0);           
             }
             }
            String urlString = value+"/upload_file.php";
            try
            {
             //------------------ CLIENT REQUEST
                UUID uniqueKey = UUID.randomUUID();
                fname = uniqueKey.toString();
                Log.e("UNIQUE NAME",fname);

            FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            dos = new DataOutputStream( conn.getOutputStream() );
             dos.writeBytes(twoHyphens + boundary + lineEnd);
             dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fname + "."+extension+"" + lineEnd);
             dos.writeBytes(lineEnd);
             bytesAvailable = fileInputStream.available();
             System.out.println("BYTES:--------->"+bytesAvailable);
             bufferSize = Math.min(bytesAvailable, maxBufferSize);
             System.out.println("BUFFER SIZE:--------->"+bufferSize);
             buffer = new byte[bufferSize];
             System.out.println("BUFFER:--------->"+buffer);
             bytesRead = fileInputStream.read(buffer,0,bufferSize);
             System.out.println("BYTES READ:--------->"+bytesRead);
             while (bytesRead > 0)
             {
              dos.write(buffer, 0, bufferSize);
              bytesAvailable = fileInputStream.available();
              bufferSize = Math.min(bytesAvailable, maxBufferSize);
              bytesRead = fileInputStream.read(buffer, 0, bufferSize);
              System.out.println("RETURNED");
             }
             dos.writeBytes(lineEnd);
             dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

             Log.e("Debug","File is written");
             fileInputStream.close();
             dos.flush();
             dos.close();

            }
            catch (MalformedURLException ex)
            {
                 Log.e("Debug", "error: " + ex.getMessage(), ex);
            }
            catch (IOException ioe)
            {
                 Log.e("Debug", "error: " + ioe.getMessage(), ioe);
            }
            //------------------ read the SERVER RESPONSE
            try {
                  inStream = new DataInputStream ( conn.getInputStream() );
                  String str;

                  while (( str = inStream.readLine()) != null)
                  {
                       Log.e("Debug","Server Response "+str);
                  }
                  inStream.close();

            }
            catch (IOException ioex){
             Log.e("Debug", "error: " + ioex.getMessage(), ioex);
            }

【问题讨论】:

    标签: android


    【解决方案1】:

    在您需要的地方致电new loadVideo().execute();。 并添加以下类来进行视频加载。

    public class loadVideo extends AsyncTask<Void, Void, Void>
        {
    
            private final ProgressDialog dialog = new ProgressDialog(
                    YourActivity.this);
            protected void onPreExecute() {
                this.dialog.setMessage("Loading...");
                this.dialog.setCancelable(false);
                this.dialog.show();
            }
            protected void onPostExecute(Void result) {
    
                if (this.dialog.isShowing()) {
                    this.dialog.dismiss();
                }
    
                    }
                });
    
    
    
            }
    
            @Override
            protected Void doInBackground(Void... params) {
     HttpURLConnection conn = null;
                DataOutputStream dos = null;
                DataInputStream inStream = null;
                String lineEnd = "\r\n";
                String twoHyphens = "--";
                String boundary =  "*****";
                int bytesRead, bytesAvailable, bufferSize;
                byte[] buffer;
                int maxBufferSize = 8*1024*1024;
                Cursor c = (MainscreenActivity.JEEMAHWDroidDB).query((MainscreenActivity.TABLE_Name), new String[] {
                         (MainscreenActivity.COL_HwdXml)}, null, null, null, null,
                                  null);
                 if(c.getCount()!=0){
                 c.moveToLast();
                 for(int i=c.getCount()-1; i>=0; i--) {
                      value=c.getString(0);           
                 }
                 }
                String urlString = value+"/upload_file.php";
                try
                {
                 //------------------ CLIENT REQUEST
                    UUID uniqueKey = UUID.randomUUID();
                    fname = uniqueKey.toString();
                    Log.e("UNIQUE NAME",fname);
    
                FileInputStream fileInputStream = new FileInputStream(new File(selectedPath) );
                URL url = new URL(urlString);
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
                dos = new DataOutputStream( conn.getOutputStream() );
                 dos.writeBytes(twoHyphens + boundary + lineEnd);
                 dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fname + "."+extension+"" + lineEnd);
                 dos.writeBytes(lineEnd);
                 bytesAvailable = fileInputStream.available();
                 System.out.println("BYTES:--------->"+bytesAvailable);
                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
                 System.out.println("BUFFER SIZE:--------->"+bufferSize);
                 buffer = new byte[bufferSize];
                 System.out.println("BUFFER:--------->"+buffer);
                 bytesRead = fileInputStream.read(buffer,0,bufferSize);
                 System.out.println("BYTES READ:--------->"+bytesRead);
                 while (bytesRead > 0)
                 {
                  dos.write(buffer, 0, bufferSize);
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                  System.out.println("RETURNED");
                 }
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
                 Log.e("Debug","File is written");
                 fileInputStream.close();
                 dos.flush();
                 dos.close();
    
                }
                catch (MalformedURLException ex)
                {
                     Log.e("Debug", "error: " + ex.getMessage(), ex);
                }
                catch (IOException ioe)
                {
                     Log.e("Debug", "error: " + ioe.getMessage(), ioe);
                }
                //------------------ read the SERVER RESPONSE
                try {
                      inStream = new DataInputStream ( conn.getInputStream() );
                      String str;
    
                      while (( str = inStream.readLine()) != null)
                      {
                           Log.e("Debug","Server Response "+str);
                      }
                      inStream.close();
    
                }
                catch (IOException ioex){
                 Log.e("Debug", "error: " + ioex.getMessage(), ioex);
                }
    
                return null;
            }
    
        }
    

    【讨论】:

    • 请告诉我如何调用 loadVideo 类?我是 android manikandan 的新手。那是你问的
    • 这里的进度条什么时候消失?
    • 嗨,我想在通知区域显示进度条..可以使用 Asynctask 完成吗?
    • 您无法使用 AsyncTask 在通知区域显示进度条。请点击此链接united-coders.com/nico-heid/…
    • 你可以从这个github站点github.com/nheid/unitedcoders-android获取样本
    【解决方案2】:

    在开始网络通信之前进行此调用...

    ProgressDialog dialog = ProgressDialog.show(this, "", "Loading"); 
    

    完成后调用下面的..

    dialog.dismiss();
    

    ...

    最好使用AsyncTask进行网络通信

    【讨论】:

    • 我是 android 新手。我对 AsyncTask 不太熟悉。如果您有任何相关的代码,请发给我...
    • this。您将获得更多信息……然后您可以按照自己的方式进行开发……
    • 不要在 doInBackGround() 方法中编写任何反映 UI 的代码。而是写 onPostExecute()。
    • 干杯............如果你有解决方案,请作为接受的答案。这停止了​​这个问题的更多答案。
    • 嗨,我想在通知区域显示进度条..可以使用 Asynctask 完成吗?
    【解决方案3】:

    This 问题可能会对您有所帮助。基本上你需要在AsyncTaskonPreExecute() 方法中创建一个ProgressDialog 并在onPostExecute() 中将其关闭。
    上传代码将进入AsyncTaskdoInBackGround()方法。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2023-03-30
    • 2014-08-17
    • 2012-12-11
    相关资源
    最近更新 更多