Skip to content

Instantly share code, notes, and snippets.

@jab416171
Created May 16, 2013 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jab416171/5590211 to your computer and use it in GitHub Desktop.
Save jab416171/5590211 to your computer and use it in GitHub Desktop.
private static class ASyncUpload extends AsyncTask<FileInputStream, Integer, String> {
private NotificationManager mNotifyManager;
private NotificationCompat.Builder mBuilder;
public DocumentUploadAsync(Context ctxt) {
mNotifyManager =
(NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(ctxt);
mBuilder.setContentTitle("File Upload")
.setContentText("Upload in progress");
}
@Override
protected void onProgressUpdate(Integer... values) {
int progress = values[0];
mBuilder.setProgress(100, (int) progress, false);
mNotifyManager.notify(6, mBuilder.build());
if(progress == 100) {
mBuilder.setContentText("Upload complete").setProgress(0,0,false);
mNotifyManager.notify(6, mBuilder.build());
}
}
@Override
protected String doInBackground(FileInputStream... params) {
FileInputStream fis = params[0];
byte[] buffer = new byte[1024];
for (int i=0, length = 0; (length = fis.read(buffer)) > 0; i++) {
double percentage = 100*((i*1024.0)/fileLength);
if(i%100 == 0) {
publishProgress((int)percentage);
}
out.write(buffer, 0, length);
out.flush();
}
publishProgress(100);
out.close();
Scanner in = new Scanner(uploadConnection.getInputStream());
json = in.nextLine();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
@Override
protected void onPostExecute(String result) {
if (result == null || "".equals(result)) {
Log.d("asdf", "Result is empty");
}
createDocument(result, fileName, work);
Log.v("asdf", "Document uploaded!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment