Skip to content

Instantly share code, notes, and snippets.

@Vikctar
Created August 19, 2017 19:10
Show Gist options
  • Save Vikctar/e209e9cc12e2cfd3734fef6bb06d7e7f to your computer and use it in GitHub Desktop.
Save Vikctar/e209e9cc12e2cfd3734fef6bb06d7e7f to your computer and use it in GitHub Desktop.
upload image
/**
* 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