Hi Iam doing an android aplication in which iam uploading videos to PHP server.
Iam using HTTPURLConnection
to do the upload. Iam strucked in showing progress bar in notification area and updating it.
Iam searching almost a week to do this.But can't get a hint.Please help me if anybody knows:
My code:
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead;
byte[] buffer;
String urlString = "http://xxxxx/My_path.php";
try {
long total = 0;
int count = 0;
// ------------------ 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();
int length=selectedPath.length();
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 + "" + lineEnd);
dos.writeBytes(lineEnd);
buffer = new byte[8192];
bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) > 0) {
dos.write(buffer, 0, bytesRead);
}
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);
}
In RemoveView, you can update the Progress bar. So combining some of the example codes, I get something like this:
This blog post has a very good example of implementing the same. Hope this helps.
A much simpler way using
setProgress
:After a long search i finished the task of showing progress bar in notification area.All we need is
asynctask
.May be the code iam showing here will not work perfectly,It works fine when iam testing.Please check and upvote for this answer if it works good.My code:
Thanks and Regards Sundar.