Skip to content

Instantly share code, notes, and snippets.

@haerulmuttaqin
Created June 8, 2018 16:35
Show Gist options
  • Save haerulmuttaqin/1ae6a3667adfb0b46ee31177289f8a57 to your computer and use it in GitHub Desktop.
Save haerulmuttaqin/1ae6a3667adfb0b46ee31177289f8a57 to your computer and use it in GitHub Desktop.
additional, to display an image
additional,
To display an image:
/**
you have to send the image url from server, you change php code (read_detail.php) following this:
**/
//read_detail.php
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$id = $_POST['id'];
require_once 'connect.php';
$sql = "SELECT * FROM users_table WHERE id='$id' ";
$response = mysqli_query($conn, $sql);
$result = array();
$result['read'] = array();
if( mysqli_num_rows($response) === 1 ) {
if ($row = mysqli_fetch_assoc($response)) {
$h['name'] = $row['name'] ;
$h['email'] = $row['email'] ;
$h['image'] = $row['photo']; //add this
array_push($result["read"], $h);
$result["success"] = "1";
echo json_encode($result);
}
}
}else {
$result["success"] = "0";
$result["message"] = "Error!";
echo json_encode($result);
mysqli_close($conn);
}
?>
//Add build.gradle
implementation 'com.squareup.picasso:picasso:2.71828'
/**
url image sent from server with json format, and json object (image) must be accepted using method getUserDetail ()
**/
//Edit getUserDetail like this
private void getUserDetail(){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_READ,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
Log.i(TAG, response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("read");
if (success.equals("1")){
for (int i =0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String strName = object.getString("name").trim();
String strEmail = object.getString("email").trim();
//Add this for fetch image from json
String strImage = object.getString("image").trim();
name.setText(strName);
email.setText(strEmail);
//display image from string url
Picasso.get().load(strImage).into(profile_image);
}
}
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
Toast.makeText(HomeActivity.this, "Error Reading Detail "+e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(HomeActivity.this, "Error Reading Detail "+error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String > params = new HashMap<>();
params.put("id", getId);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
/**
sorry i have forgot to add, display the picture from the server.
**/
@jaihind1702
Copy link

pls tell us? how to do this??

@clickeatapp
Copy link

@haerulmuttaqin, sir i got no error but image is not loading in circle image. I really appriciate it if you make a time to solve this.

@Xplizit4499
Copy link

@haerulmuttaqin, sir i got no error but image is not loading in circle image. I really appriciate it if you make a time to solve this.

i also have the same problem. how to fix it?

@SynowiecKamil
Copy link

@haerulmuttaqin, sir i got no error but image is not loading in circle image. I really appriciate it if you make a time to solve this.

i also have the same problem. how to fix it?

Add this and import classes:

Picasso.get().load(strImage)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(profile_image);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment