1

アプリから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);
            }
4

3 に答える 3

0

ネットワーク通信を開始する前にこの電話をかけてください...

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

完了後、以下に電話してください。

dialog.dismiss();

..。

ネットワーク通信にはAsyncTaskを使用する方が良い

于 2012-04-16T11:38:37.247 に答える
0

この質問はあなたを助けるかもしれません。基本的に、のProgressDialoginonPreExecute()メソッドを作成し、でAsyncTaskそれを却下する必要がありますonPostExecute()
アップロードコードはのdoInBackGround()メソッドになりAsyncTaskます。

于 2012-04-16T11:40:24.490 に答える
0

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

    }
于 2012-04-16T11:51:03.490 に答える