diff options
Diffstat (limited to 'app/src/main/java/com/lc8n/util/FileUploader.java')
-rw-r--r-- | app/src/main/java/com/lc8n/util/FileUploader.java | 105 |
1 files changed, 105 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(); + } +} |