Created
August 19, 2017 19:10
-
-
Save Vikctar/e209e9cc12e2cfd3734fef6bb06d7e7f to your computer and use it in GitHub Desktop.
upload image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Convert image bitmap to base64 string | |
* @param bmp bitmap | |
* @return base64 string | |
*/ | |
public String getStringImage(Bitmap bmp) { | |
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | |
bmp.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); | |
byte[] imageBytes = byteArrayOutputStream.toByteArray(); | |
return "data:image/jpeg;base64," + Base64.encodeToString(imageBytes, Base64.DEFAULT); | |
} | |
private void uploadImage() { | |
progressBar.setVisibility(View.VISIBLE); | |
Log.d("Image upload", "started"); | |
StringRequest request = new StringRequest(Request.Method.POST, "https://fundilistapp.com/api/v1/sp_avatar", | |
new Response.Listener<String>() { | |
@Override | |
public void onResponse(String s) { | |
Log.d("Upload image", s); | |
progressBar.setVisibility(View.GONE); | |
try { | |
JSONObject jsonObject = new JSONObject(s); | |
boolean error = jsonObject.getBoolean("error"); | |
if (!error) { | |
String avatarUrl = jsonObject.getString("avatar"); | |
accountSharedPrefs.setAvatarUrl(avatarUrl); | |
Log.d("Edit Profile Response", "Success"); | |
}getActivity().finish(); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
}, new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError volleyError) { | |
progressBar.setVisibility(View.GONE); | |
getActivity().finish(); | |
} | |
}) { | |
@Override | |
protected Map<String, String> getParams() throws AuthFailureError { | |
String image = getStringImage(bitmap); | |
Map<String, String> params = new HashMap<>(); | |
params.put("service_provider_id", providerId); | |
params.put("image", image); | |
return params; | |
} | |
}; | |
AppController.getInstance().addToRequestQueue(request); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment