在Android上传进度条?

我使用下面的代码上传video到PHP服务器,它工作正常,但我需要显示进度栏,而file upload时,我需要做file upload和进度栏增量同步。任何人都build议一些想法?

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 = 1 * 1024 * 1024; String responseFromServer = ""; File sourceFile = new File(sourceFileUri); if (!sourceFile.isFile()) { Log.e("Huzza", "Source File Does not exist"); return; } int serverResponseCode=0; try { // open a URL connection to the Servlet FileInputStream fileInputStream = new FileInputStream(sourceFile); URL url = new URL(upLoadServerUri); conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); conn.setRequestProperty("uploadedfile", fileName); // conn.setFixedLengthStreamingMode(1024); //conn.setChunkedStreamingMode(1); dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+ fileName + "\"" + lineEnd); dos.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); // create a buffer of maximum size Log.i("Huzza", "Initial .available : " + bytesAvailable); //bufferSize = Math.min(bytesAvailable, maxBufferSize); bufferSize=(int)sourceFile.length(); System.out.println("BytesAvail"+bytesAvailable); System.out.println("maxBufferSize"+maxBufferSize); buffer = new byte[bufferSize]; // read file and write it into form... bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // send multipart form data necesssary after file data... dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); // close streams Log.i("Upload file to server", fileName + " File is written"); fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { ex.printStackTrace(); Log.e("Upload file to server", "error: " + ex.getMessage(), ex); } catch (Exception e) { e.printStackTrace(); } //this block will give the response of upload link try { BufferedReader rd = new BufferedReader(new InputStreamReader(conn .getInputStream())); String line; while ((line = rd.readLine()) != null) { Log.i("Huzza", "RES Message: " + line); } rd.close(); } catch (IOException ioex) { Log.e("Huzza", "error: " + ioex.getMessage(), ioex); } return; // like 200 (Ok) 

谢谢 。

Solutions Collecting From Web of "在Android上传进度条?"

把你的上传代码放在AsyncTaskdoInBackGround方法中。 然后,您可以从这里发布进度,并在onProgressUpdate更新ProgressBar。

在这里看看这可能看起来publishProgress

我想你问的是如何获得当前上传进度的数字表示,以便您可以将该值显示为进度条。 用你现在的代码,我不确定你能轻松的。 什么是您尝试上传的最大的文件? 我认为你会得到一个大文件的OutOfMemory错误。 你做这个:

 byte[] buffer; bufferSize=(int)sourceFile.length(); buffer = new byte[bufferSize]; bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { ... 

你的“缓冲区”只是一个与整个文件大小相同的数组。 调用fileInputStream.read()可以尝试将文件的全部内容放入“缓冲区”。 如果您的文件很大(超过几十兆字节),那么您的应用程序可能会因OutOfMemory错误而崩溃。

如果使用固定大小的小缓冲区,那么每次通过while循环时,都可以计算已经发送的数据量,将其转换为百分比值,并为AsyncTask调用publishProgess或为Handler调用sendMessage

或者你可以使用一个处理程序的线程,如果你想要更细粒度的控制,但对于大多数任务AFAIK AsyncTask应该工作。

 connection.setFixedLengthStreamingMode((int) requestLength); 

为我做了诡计!