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