Jon Stark Jon Stark - 1 month ago 14
Android Question

Picasso, Image view is not getting refreshed

I am using Picasso library to load a image into image view, at the very first time it works fine but after that it repeats the same image again if i go back and with some changes launch the activity with the image again.The url is always the same, I have searched and applied many changes its not working please help.

At the same time when i close the app and restart it then also its not getting refreshed and when i hit the url on the browser it shows me the updated image.

Picasso.with(this)
.load(Config.API_URL+"/sites/default/files/"+uiidd+".png")
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(imageView);






public class Activity_result extends AppCompatActivity {
Toolbar toolbar;
private ImageView imageView;
String uiidd;

Button testagain_btn;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);

imageView = (ImageView) findViewById(R.id.imageView_chart);
toolbar = (Toolbar) findViewById(R.id.tool_barresult);
testagain_btn=(Button)findViewById(R.id.btn_testagain);



setSupportActionBar(toolbar);
uiidd = MyApplication.getInstance().getUid();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.color_darkgreen));
}

Picasso.with(this)
.load(Config.API_URL+"/sites/default/files/"+uiidd+".png")
.into(imageView);
/* new Thread(new Runnable() {
@Override
public void run() {
Glide.get(Activity_result.this).clearDiskCache();
}
}).start();

Glide.with(this)
.load(Config.API_URL+"/sites/default/files/"+uiidd+".png")
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
//.signature(new StringSignature(System.currentTimeMillis()))
.into(imageView);*/

resultjsonrequest();
testagain_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Intent i = new Intent(Activity_result.this,Activity_knowStatus.class);
startActivity(i);*/

final Dialog dialog = new Dialog(Activity_result.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialoghealthdata);

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

ImageView dialogButtonCancel = (ImageView) dialog.findViewById(R.id.imageView_close);
Button dialogButtonOk = (Button) dialog.findViewById(R.id.btn_ok);
// Click cancel to dismiss android custom dialog box
dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

// Your android custom dialog ok action
// Action for custom dialog ok button click
dialogButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Activity_result.this,Activity_knowStatus.class);
startActivity(i);
}
});

dialog.show();






}
});




}

@Override
protected void onResume() {
super.onResume();
Picasso.with(this)
.load(Config.API_URL+"/sites/default/files/"+uiidd+".png")
.into(imageView);
}

/* @Override
public void onBackPressed()
{

finish();

}*/

private void resultjsonrequest() {

Util.showProDialog(Activity_result.this,"Loading....");

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
Config.API_URL+"/profile-result-json?userProfileID="+uiidd+"", null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {


try {

String txt_totalscore1 = response.getString("patient_score");
txtvw_totalscore.setText(txt_totalscore1);

String txt_chol1 = response.getString("Cholesterol");
txt_chol.setText(txt_chol1);
String cholcolor = response.getString("cholesterolPointColor");
linear_chol.setBackgroundColor(Color.parseColor(cholcolor));

String txt_trigly1 = response.getString("Triglycerides");
txt_trigly.setText(txt_trigly1);
String triglycolor = response.getString("triglycerideScoreColor");
linear_trigly.setBackgroundColor(Color.parseColor(triglycolor));

String txt_hdlchol1 = response.getString("HDLCholesterol %");
txt_hdl_chol.setText(txt_hdlchol1);
String hdlchl1color = response.getString("hDL_cholesterol_scoreColor");
linear_hdl_chol.setBackgroundColor(Color.parseColor(hdlchl1color));

String txt_sysbp1 = response.getString("SystolicBP");
txt_systolicbp.setText(txt_sysbp1);
String systoiccolor = response.getString("systolic_bp_scoreColor");
linear_systolicbp.setBackgroundColor(Color.parseColor(systoiccolor));

String txt_disys1 = response.getString("DiastolicBP");
txt_disystolicbp.setText(txt_disys1);
String disyscolor = response.getString("diastolic_bp_scoreColor");
linear_disystolicbp.setBackgroundColor(Color.parseColor(disyscolor));

String txsugarf1 = response.getString("Blood Sugar Fasting (FF)");
txt_bloodsugarff.setText(txsugarf1);
String sugarfcolor = response.getString("bl_glucode_ff_scoreColor");
linear_bloodsugarff.setBackgroundColor(Color.parseColor(sugarfcolor));


String txsugarp1 = response.getString("Blood Sugar (PP)");
txt_bloodsugarpp.setText(txsugarp1);
String sugarpcolor = response.getString("bl_glucose_pp_scoreColor");
linear_bloodsugarpp.setBackgroundColor(Color.parseColor(sugarpcolor));

String txsmoking1 = response.getString("Smoking");
txt_smoking.setText(txsmoking1);
String smokingcolor = response.getString("Smoking_ScoreColor");
linear_smoking.setBackgroundColor(Color.parseColor(smokingcolor));


String txbmi = response.getString("BMI");
txt_bmi.setText(txbmi);
String bmicolor = response.getString("BMI_ScoreColor");
linear_bmi.setBackgroundColor(Color.parseColor(bmicolor));

String txwalking1 = response.getString("Walking");
txt_walking.setText(txwalking1);
String walkingcolor = response.getString("walkingPointColor");
linear_walking.setBackgroundColor(Color.parseColor(walkingcolor));

String txt_oil1 = response.getString("Oil Intake");
txt_oil.setText(txt_oil1);
String oilcolor = response.getString("oil_ghee_intake_scoreColor");
linear_oil.setBackgroundColor(Color.parseColor(oilcolor));


String txmilk1 = response.getString("MilkDairy Intake");
txt_milk.setText(txmilk1);
String milkcolor = response.getString("milk_dairy_intake_scoreColor");
linear_milk.setBackgroundColor(Color.parseColor(milkcolor));

String txfruit1 = response.getString("Fruits Intake");
txt_fruit.setText(txfruit1);
String fruitcolor = response.getString("Vegitable_ScoreColor");
linear_fruit.setBackgroundColor(Color.parseColor(fruitcolor));

String txt_vegetable1 = response.getString("Vegetables Intake");
txt_vegetable.setText(txt_vegetable1);
String vegetablecolor = response.getString("vegetable_you_intake_scoreColor");
linear_vegetable.setBackgroundColor(Color.parseColor(vegetablecolor));

String txnonveg1 = response.getString("Animal Food");
txt_nonveg.setText(txnonveg1);
String nonvegcolor = response.getString("intake_non_veg_scoreColor");
linear_nonveg.setBackgroundColor(Color.parseColor(nonvegcolor));

String txyoga1 = response.getString("Yoga");
txt_yoga.setText(txyoga1);
String yogacolor = response.getString("yoga_or_stretching_exercis_scoreColor");
linear_yoga.setBackgroundColor(Color.parseColor(yogacolor));

String txmeditation1 = response.getString("MeditationPranayam");
txt_meditation.setText(txmeditation1);
String meditationcolor = response.getString("meditation_prayer_scoreColor");
linear_meditation.setBackgroundColor(Color.parseColor(meditationcolor));

String txstress1 = response.getString("Stress Management");
txt_stress.setText(txstress1);
String stresscolor = response.getString("stress_mgmt_scoreColor");
linear_stress.setBackgroundColor(Color.parseColor(stresscolor));






} catch (JSONException e) {
e.printStackTrace();
Util.showMessageDialog(Activity_result.this, "Server error! Try Again");
}
Util.dimissProDialog();
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Util.dimissProDialog();
Util.showMessageDialog(Activity_result.this, "Server error! Try Again");
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

// Adding request to request queue
// MyApplication.getInstance().addToRequestQueue(jsonObjReq);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjReq);
}






}

Answer

I have got the solution of the above problem, The main reason of the problem was the same url with the updated image, so I added a token to the url with randomly generated number everytime,hence the url gets changed and the image gets updated.

    double token=0;
    token=Math.random();
    Picasso.with(getApplicationContext()).invalidate("");
    Picasso.with(this)
                 .load(Config.API_URL+"/sites/default/files/"+uiidd+".png?"+token)
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .into(imageView_chartt);