neha
neha

Reputation: 117

Android Profile Image

I have made an Activity in which user picks the image from gallery and set this image in circular ImageView, here is my part of code:

public void loadImagefromGallery(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);      //camera intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);


}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
    if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
            && null != data) {
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        cursor.close();
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
        //  imgView = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.ivprofileimg);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] byteArrayImage = baos.toByteArray();
        encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
        new saveprofileimage().execute(custid, encodedImage);

    } else {
        Toast.makeText(this, "You haven't picked Image",
                Toast.LENGTH_LONG).show();
    }

} catch (Exception e) {
    Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
            .show();
}

}


class saveprofileimage extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
    pdilog = new ProgressDialog(MyProfile.this);
    //     pdilog.setMessage("Please Wait....");
    pdilog.show();
    super.onPreExecute();
}


@Override
protected Void doInBackground(String... params) {
    try {
        response = SaveProfileImage.getJSONfromURL(params[0] + " ", params[1]);
        JSONObject _jobject = new JSONObject(response);
        serverresult = _jobject.getString("Result");
        profileimagee = _jobject.getString("ProfileImage");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}


@Override
protected void onPostExecute(Void result) {
    pdilog.dismiss();
    if (serverresult.equals("Success")) {
        Picasso.with(MyProfile.this)
                .load(profileimagee)
                .into(imgView);
        new customerprofiledetails().execute(custid);
    } else {
        Toast.makeText(MyProfile.this, "Image couldn't be saved", Toast.LENGTH_LONG).show();
    }

    super.onPostExecute(result);

}
}

The problem is that when the image is already there in the ImageView and after that I try to update the image, the image does not get updated but when the user closes and reopens the app, the updated image is there in the ImageView, I want is whenever I tap on the ImageView and chooses an image it gets updated on the spot in just few seconds,there should not be any need to open the app again, Suggest me asap

Upvotes: 0

Views: 225

Answers (4)

A.Arjun
A.Arjun

Reputation: 105

clear cache of picasso. If you do not clear cache memory, it loads the image from cache memory. or try this code:

Glide.with(getApplicationContext()).load("image_url")
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.listener(new RequestListener<String, GlideDrawable>() {
    @Override
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        progressBar.setVisibility(View.GONE);
        return false;
    }

    @Override
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        progressBar.setVisibility(View.GONE);
        return false;
    }
})

.into(profileImage);

Upvotes: 2

Aswin Arshad
Aswin Arshad

Reputation: 90

It is because of the local cache residing in android memory. Try clearing up the cache.

Upvotes: 2

Fahim Al Mahmud Ashik
Fahim Al Mahmud Ashik

Reputation: 1121

Inside your onPostExecute just modify it like below

    @Override
    protected void onPostExecute(Void result) {
        pdilog.dismiss();
        if (serverresult.equals("Success")) {
            Picasso.with(MyProfile.this)
                    .load(profileimagee)
                    .into(imgView);
       finish();
       startActivity(getApplicationContext(),YourActivityName.class);
       }

Upvotes: 1

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this:

 public class PicassoTools {

    public static void clearCache (Picasso p) {
        p.cache.clear();
    }
}

call it using:

PicassoTools.clearCache(Picasso.with(context));

you can also try:

 Picasso.with(MyProfile.this)
        .load(profileimagee)
        .memoryPolicy(MemoryPolicy.NO_CACHE )
        .networkPolicy(NetworkPolicy.NO_CACHE)
        .into(imgView);

it will automatically refresh the file..

Upvotes: 2

Related Questions