In my android eclipse project, i want to upload image file with name and email fields to server.
But i got the following error : my logcat is following :
<
This is main problem of libs., that all might faced, wrong library causes this issue,
So, following step helpful to you :
(1) Code : Following code is successfully upload photo to the server :
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (Constant.bitmapPicture != null) {
StoreByteImage(Constant.bitmapPicture, 90, "my_image");
} else {
Toast.makeText(getApplicationContext(), "image null", Toast.LENGTH_LONG).show();
}
}
});
StoreByteImage(...) :
public boolean StoreByteImage(Bitmap bitmap, int quality, String expName) {
FileOutputStream fileOutputStream = null;
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + "/Example");
if (myNewFolder.mkdirs()) {
myNewFolder.mkdir();
}
try {
iPath = myNewFolder + "/" + expName + ".jpg";
fileOutputStream = new FileOutputStream(myNewFolder + "/" + expName + ".jpg");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bitmap.compress(CompressFormat.JPEG, quality, bos);
new ImageUploadTask().execute();
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Class ImageUploadTask
class ImageUploadTask extends AsyncTask<Void, Void, String> {
String sResponse;
@SuppressWarnings("deprecation")
@Override
protected String doInBackground(Void... unsued) {
try {
//
File image = new File(iPath);
FileBody fileBody = new FileBody(image);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(YourImageUploadURLHere);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Constant.bitmapPicture.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("ID", new StringBody("1"));
entity.addPart("pID", new StringBody("1"));
entity.addPart("userPhoto", fileBody);
entity.addPart("email", new StringBody("email@gmail.com"));
entity.addPart("cell", new StringBody("1234567890"));
entity.addPart("username", new StringBody("name"));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
"UTF-8"));
sResponse = reader.readLine();
System.out.println("responce=" + sResponse);
} catch (Exception e) {
System.out.println("error=" + e.getMessage());
return null;
}
return "";
}
@Override
protected void onProgressUpdate(Void... unsued) {
}
@Override
protected void onPostExecute(String sResponses) {
try {
if (sResponses != null) {
JSONObject jsonObjSend = new JSONObject(sResponse.toString());
if (jsonObjSend.getString("status").equals("success")) {
// another code
} else if (jsonObjSend.getString("status").equals("fail")) {
// another code
}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "excepttion", Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
(2) First Remove All libs/jar file from the your libs Folder :
(3) Find Following libraries/jar file from the internet and put in libs folder :
(4) Add jar from the libraries tab :
(5) Your Order and Export libraries/jar file Tick mark same as following :
Ok, Done. Just these simple steps helpful to you upload image file to server. hope these helphul to you and others.
I have upgraded from 4.3 to 4.4 before i have noticed this error. Could be 4.3 causing it.