Below is my code for uploading a file to the server. But I\'m getting network exceptions even after several tries and even after adding strict mode.
I\'m new to Andr
/**
* *******Async Task for Use this UTILITY CLASS *
* pass the file which need to upload *
* Progress dialog commente/ Uncomment according requirment*******/
private class ImageUploaderTask extends AsyncTask<String, Integer, Void> {
@Override
protected void onPreExecute(){
// simpleWaitDialog = ProgressDialog.show(BlogPostExamplesActivity.this, "Wait", "Uploading Image");
}
@Override
protected Void doInBackground(String... params) {
new ImageUploadUtility().uploadSingleImage(params[0]);
return null;
}
@Override
protected void onPostExecute(Void result){
// simpleWaitDialog.dismiss();
}
/**
* Method uploads the image using http multipart form data.
* We are not using the default httpclient coming with android we are using the new from apache
* they are placed in libs folder of the application
*
* @param imageData
* @param filename
* @return
* @throws Exception
*/
static boolean doUploadinBackground(final byte[] imageData, String filename) throws Exception{
String responseString = null;
PostMethod method;
method = new PostMethod("your url to upload");
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(
100000);
FilePart photo = new FilePart("userfile", new ByteArrayPartSource(
filename, imageData));
photo.setContentType("image/jpeg");
photo.setCharSet(null);
String s = new String(imageData);
Part[] parts = {
new StringPart("latitude", "123456"),
new StringPart("longitude","12.123567"),
new StringPart("imei","1234567899"),
new StringPart("to_email","some email"),
photo
};
method.setRequestEntity(new MultipartRequestEntity(parts, method
.getParams()));
client.executeMethod(method);
responseString = method.getResponseBodyAsString();
method.releaseConnection();
Log.e("httpPost", "Response status: " + responseString);
if (responseString.equals("SUCCESS")) {
return true;
} else {
return false;
}
}
/**
* Simple Reads the image file and converts them to Bytes
*
* @param file name of the file
* @return byte array which is converted from the image
* @throws IOException
*/
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
}
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new UploadImageTask().execute(); // initialize asynchronous task
}});
//Now implement Asynchronous Task
public class Get_User_Data extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
MyActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
uploadImage(); // inside the method paste your file uploading code
return null;
}
protected void onPostExecute(Void result) {
// Here if you wish to do future process for ex. move to another activity do here
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
For more information refer this link http://vikaskanani.wordpress.com/2011/01/29/android-image-upload-activity/