summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/lc8n/util
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/lc8n/util')
-rw-r--r--app/src/main/java/com/lc8n/util/FileUploader.java105
-rw-r--r--app/src/main/java/com/lc8n/util/ProgressMultipartEntity.java72
2 files changed, 177 insertions, 0 deletions
diff --git a/app/src/main/java/com/lc8n/util/FileUploader.java b/app/src/main/java/com/lc8n/util/FileUploader.java
new file mode 100644
index 0000000..6adf229
--- /dev/null
+++ b/app/src/main/java/com/lc8n/util/FileUploader.java
@@ -0,0 +1,105 @@
+/**
+ *
+ */
+package com.lc8n.util;
+
+import android.app.ProgressDialog;
+import android.content.Context;
+import android.os.AsyncTask;
+import android.util.Log;
+
+import com.lc8n.util.ProgressMultipartEntity.ProgressListener;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.util.EntityUtils;
+
+import java.io.File;
+
+
+/**
+ * @author joe
+ *
+ */
+public class FileUploader extends AsyncTask<File, Integer, String> {
+
+ ProgressDialog pd;
+ int totalSize;
+ protected Context mContext;
+
+ public FileUploader(Context context) {
+
+ mContext = context;
+ }
+ @Override
+ protected void onPreExecute()
+ {
+
+ }
+
+ public void prepare(int max) {
+ pd = new ProgressDialog(mContext);
+ pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
+ pd.setMessage("Uploading File...");
+ pd.setCancelable(false);
+ pd.setMax(max);
+ pd.show();
+ }
+
+ @Override
+ protected String doInBackground(File... file) {
+
+ HttpClient httpClient = new DefaultHttpClient();
+ HttpContext httpContext = new BasicHttpContext();
+ HttpPost httpPost = new HttpPost("http://up.org.je/upload_file.php");
+ Log.d("File", file[0].getName());
+
+ try
+ {
+ ProgressMultipartEntity multipartContent = new ProgressMultipartEntity(new ProgressListener()
+ {
+
+ public void transferred(long num)
+ {
+ publishProgress((int)(num/1024));
+ }
+ });
+
+ // We use FileBody to transfer an image
+ multipartContent.addPart("file", new FileBody(file[0]));
+
+ totalSize = (int)(multipartContent.getContentLength()/1024);
+ Log.d("Size", String.valueOf(totalSize));
+ // Send it
+ httpPost.setEntity(multipartContent);
+ HttpResponse response = httpClient.execute(httpPost, httpContext);
+ String serverResponse = EntityUtils.toString(response.getEntity());
+
+ return serverResponse;
+ }
+
+ catch (Exception e)
+ {
+ Log.e("Upload error", e.getMessage());
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ @Override
+ protected void onProgressUpdate(Integer... progress)
+ {
+ pd.setProgress((int) (progress[0]));
+ }
+
+ @Override
+ protected void onPostExecute(String a)
+ {
+ pd.dismiss();
+ }
+}
diff --git a/app/src/main/java/com/lc8n/util/ProgressMultipartEntity.java b/app/src/main/java/com/lc8n/util/ProgressMultipartEntity.java
new file mode 100644
index 0000000..21142fd
--- /dev/null
+++ b/app/src/main/java/com/lc8n/util/ProgressMultipartEntity.java
@@ -0,0 +1,72 @@
+package com.lc8n.util;
+
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntity;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.charset.Charset;
+
+public class ProgressMultipartEntity extends MultipartEntity
+{
+
+ private final ProgressListener listener;
+
+ public ProgressMultipartEntity(final ProgressListener listener)
+ {
+ super();
+ this.listener = listener;
+ }
+
+ public ProgressMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener)
+ {
+ super(mode);
+ this.listener = listener;
+ }
+
+ public ProgressMultipartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener)
+ {
+ super(mode, boundary, charset);
+ this.listener = listener;
+ }
+
+ @Override
+ public void writeTo(final OutputStream outstream) throws IOException
+ {
+ super.writeTo(new CountingOutputStream(outstream, this.listener));
+ }
+
+ public static interface ProgressListener
+ {
+ void transferred(long num);
+ }
+
+ public static class CountingOutputStream extends FilterOutputStream
+ {
+
+ private final ProgressListener listener;
+ private long transferred;
+
+ public CountingOutputStream(final OutputStream out, final ProgressListener listener)
+ {
+ super(out);
+ this.listener = listener;
+ this.transferred = 0;
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ out.write(b, off, len);
+ this.transferred += len;
+ this.listener.transferred(this.transferred);
+ }
+
+ public void write(int b) throws IOException
+ {
+ out.write(b);
+ this.transferred++;
+ this.listener.transferred(this.transferred);
+ }
+ }
+} \ No newline at end of file