Issue
Kindly asking for help, I am trying to get a JSON response from an API and the response was not null and kinda long because it has a Base64 encoded picture data This is the fragment from the response
and this is the part of the code i use to get the JSON response and convert it to JSONArray
StringRequest reqMasterDataShift = new StringRequest(Request.Method.GET, ActionUrl.LastShift+"/"+siteCode,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
JSONArray shift = jsonObject.getJSONArray("shift");
JSONArray shiftPhoto = jsonObject.getJSONArray("shift_photo"); // Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate org.json.JSONObject.toString()
Timestamp currentTime = new java.sql.Timestamp(System.currentTimeMillis());
ObjectMapper mapper = new ObjectMapper();
ArrayList<ShiftModel> shiftData = new ArrayList<>();
shiftData = mapper.readValue(shift.toString(), new TypeReference<ArrayList<ShiftModel>>() {});
ArrayList<ShiftPhotoModel> shiftPhotoData = new ArrayList<>();
shiftPhotoData = mapper.readValue(shiftPhoto.toString(), new TypeReference<ArrayList<ShiftPhotoModel>>() {});
.
.
...
I kindly hope for your help or solution for this problem, and i really appreciate for comments or some suggestion, thanks!
Solution
Check this- First convert the base64 to bitmap and try to place it in imageview
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
imageBytes = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
imageView.setImageBitmap(decodedImage);
Answered By - Sunil P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.