Reputation: 139
I convert array byte of bitmap to encodedString and send with volley in android, and have no problem in these steps.
android request code:
public static void uploadImageRequest(Bitmap bitmap, final Context context, final RequestListener requestListener) {
Map<String, String> postParam = new HashMap<>();
//convert Bitmat(image) to string.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
byte [] imageBytes = outputStream.toByteArray();
final String imageString = Base64.encodeToString(imageBytes,Base64.DEFAULT);
postParam.put("mac_address", AppUtil.getMacAddress(context));
postParam.put("token", WorkData.getData("token", context));
postParam.put("user_image" , imageString);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
Links.UPDATE_IMAGE_PROFILE_LINK, new JSONObject(postParam),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject json) {
try {
Message.messageText = json.getString("Message");
Message.messageType = json.getString("MessageType");
UserModel.isBlock = json.getBoolean("IsBlock");
UserModel.userProfileImageName = json.getString("ImageName");
requestListener.onResponse();
} catch (Exception e) {
e.printStackTrace();
requestListener.onError(e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
requestListener.onError(error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
final RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(jsonObjReq);
requestListener.onRequest();
requestQueue.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<Object>() {
@Override
public void onRequestFinished(Request<Object> request) {
requestQueue.stop();
}
});
}
in server(asp.net) I convert base64string to array byte successfuly.
my convert code
var imageBytes = Convert.FromBase64String(tblUserprofile.user_image);
and pass to the following method as parameter.
public static bool CreateImage(byte[] imageBytes)
{
try
{
using (MemoryStream ms = new MemoryStream(imageBytes))
{
using (var image = Image.FromStream(ms))
{
image.Save("~/Content/UserProfiles/picture.jpg", ImageFormat.Jpeg);
return true;
}
}
}
catch (Exception)
{
return false;
}
}
when the code arrives to image.Save(...);
, the following error occurs:
A generic error occurred in GDI+
Upvotes: 0
Views: 1311
Reputation: 3778
this happen usually (for me) when the path of the image is wrong ... check yours images paths
Upvotes: 1