is there any method for uploading images from android to php server without using any external library? if available just give me example.
2
-
If you are uploading small files like images, this could be relevant Android post Base64 String to PHPJames Wong– James Wong2014-02-17 06:23:31 +00:00Commented Feb 17, 2014 at 6:23
-
@NikolaDespotoski i tried your solution but gives error due to String image_str = Base64.encodeBytes(byte_arr); unavailablity of encodeBytes() function where i will get this librarySwapnil– Swapnil2014-02-17 06:25:48 +00:00Commented Feb 17, 2014 at 6:25
Add a comment
|
2 Answers
first convert your image into a base 64 byte array encoded string. after that send it to php. extract on server side.Then store that string in MySQL. after that send that string to android client. extract image string and decode with base 64 decode. after that you will get byte array you can simply show in your image view. for your reference I will show some code
String imagedata = Base64.encodeToString(thumbnailArray,Base64.DEFAULT);
mJobject.put("imagebyte",imagedata);
mJArray.put(mJobject);
JSONArray json=new JSONArray(response);
JSONObject jo = null;
imageArray=new String[json.length()];
imageArray[i]=jo.getString("imageid");
completeImage= Base64.decode(imageArray[0],Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(completeImage , 0, completeImage.length);
See here is complete code just try this one
AsyncTask<Void, Void, HttpEntity> editProfileTask = new AsyncTask<Void, Void, HttpEntity>() {
@Override
protected HttpEntity doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("Your url"); // your url
try {
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("firstname",new StringBody(firstnameEV.getText().toString(),
"text/plain",Charset.forName("UTF-8")));
"text/plain",Charset.forName("UTF-8")));
if (file != null) {
reqEntity.addPart("image",new FileBody(((File) file),"application/zip"));
}
httppost.setEntity(reqEntity);
HttpResponse resp = httpclient.execute(httppost);
HttpEntity resEntity = resp.getEntity();
return resEntity;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}return null;
}
@Override
protected void onPostExecute(HttpEntity resEntity) {
if (resEntity != null) {
try {
JSONObject responseJsonObject = new JSONObject(EntityUtils.toString(resEntity));
String status = responseJsonObject.getString("status");
if (status.equals("success")) {
Toast.makeText(activity, "Your Profile is updated", Toast.LENGTH_LONG).show();
String data = responseJsonObject.getString("data");
isUpdatedSuccessfully=true;
} else {
Toast.makeText(activity, "Profile not updated", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
editProfileTask.execute(null, null, null);