0

I have this code which get BLOB image that is already converted to string using webservice. This is the JSON output.

{driver_name: "Anna Biendia" taxi_plate_no: "NUV 900" driver_contact_no: "09169271825" driver_operator: "grab" driver_operator_address: "987 Buendia St. California" image: "iVBORw0KGgoAAAANSUhEUgAACDQAAAXcCAYAAADXlEzmAAAACXBIWX..."} 

This is my code in android which get the JSON and display it in the layout. The other values have been displayed except for the image.

public class DriverDetails extends Activity { ArrayList<Objects> objectsList = new ArrayList<>(); String url = "http://192.168.1.110:8080/taxisafe3/displays/taxidetails"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_driver_details); new Task().execute(url); } public class Task extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... strings) { String content = HttpULRConnect.getData(url); return content; } @Override protected void onPostExecute(String s) { try { TextView title1 = (TextView) findViewById(R.id.textView3); TextView title = (TextView) findViewById(R.id.textView2); TextView title2 = (TextView) findViewById(R.id.textView7); TextView title3 = (TextView) findViewById(R.id.textView9); TextView title4 = (TextView) findViewById(R.id.textView11); ImageView image = (ImageView) findViewById(R.id.imageView2); JSONArray ary = new JSONArray(s); for (int i = 0; i < ary.length(); i++) { JSONObject jsonobject = ary.getJSONObject(i); Objects objects = new Objects(); objects.setDriver_name(jsonobject.getString("driver_name")); objects.setTaxi_plate_no(jsonobject.getString("taxi_plate_no")); objects.setDriver_operator(jsonobject.getString("driver_operator")); objects.setDriver_operator_address(jsonobject.getString("driver_operator_address")); objects.setDriver_contact_no(jsonobject.getString("driver_contact_no")); objects.setImage(jsonobject.getString("image")); objectsList.add(objects); if (title1 != null){ title1.setText(objects.getDriver_name()); } if (title != null){ title.setText(objects.getTaxi_plate_no()); } if (title2 != null){ title2.setText(objects.getDriver_operator()); } if (title3 != null){ title3.setText(objects.getDriver_operator_address()); } if (title4 != null){ title4.setText(objects.getDriver_contact_no()); } if(image != null){ byte[] decodedString = Base64.decode(objects.getImage(), Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); image.setImageBitmap(decodedByte); } } } catch (JSONException e) { e.printStackTrace(); } } } } 

What is wrong in my code why the image doesn't display in ImageView? Thanks in advance. :)

2 Answers 2

1

After convert the base64 to bitmap

byte[] decodedString = Base64.decode(objects.getImage(), Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

resize your bitmap to Imageview height and width

image.setImageBitmap(Bitmap.createScaledBitmap(decodedByte, image.getWidth(), image.getHeight(), false)); 
Sign up to request clarification or add additional context in comments.

4 Comments

sir I tried your code but the application has stop. this is what i've done.
<code>byte[] decodedString = Base64.decode(objects.getImage(), Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); image.setImageBitmap(Bitmap.createScaledBitmap(decodedByte, image.getWidth(), image.getHeight(), false));<code>
mmmm code is work good with me , debug your code step by step using log to know when and where the crash happens
Sir. thank you very much. i misunderstood your comment. :) i already did it. :)
0

Maybe your image is missing height and width?

try using

// resize bitmap Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 720, 720, true); 

u can use 720x720 just to see if that's the problem, if it is, you can pass the desired height & width instead.

2 Comments

Thank you for your answer sir. But when i added your code below the Bitmap decodedbyte and i change image.setImageBitmap(resizedBitmap). But when i run the app, it unfornately stop.
does the size of my ImageView be the same with the size of my image?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.