jeudi 16 juin 2016

Persistent Thumbnails with PIcasso

I'm building a profile page for my app. I've made it in such a way that tapping on the previous profile image, the user will be able to change its profile picture. I would like that after the upload, the profile picture will be instantaneously updated. I've tried using Picasso but it seems to have some problems with the cache. In fact after the user has chosen his image, the picture which is shown is the same as before, despite the fact that the app overwrites the previous image file and re-apply Picasso. I'm using Android API 22.

Profile.java

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

    ImageView pic = (ImageView) findViewById(R.id.picc);


        ...

        String root = Environment.getExternalStorageDirectory().toString();
        String path = root + "/directory/name.jpg";

        MainActivity.trimCache(this);
        Picasso.with(getApplicationContext()).load(path)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                .transform(new RoundedTransformation(1000, 0))
                    .resize(500, 500)
                    .centerCrop()
                .into(pic);
    }



    pic.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent chooseImageIntent = ImagePicker.getPickImageIntent(getApplicationContext());
            startActivityForResult(chooseImageIntent, PICK_IMAGE_ID);
        }
    });

}




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            Bitmap bitmap = ImagePicker.getImageFromResult(this, resultCode, data);

            ImageView pic = (ImageView) findViewById(R.id.picc);

            RetrieveFeedTask job = new RetrieveFeedTask(data, resultCode, this, bitmap);
            job.execute("user","pass");

            String root = Environment.getExternalStorageDirectory().toString();
            String path = root + "/directory/name.jpg";
            MainActivity.trimCache(this);
            Picasso.with(getApplicationContext()).load(path)
                    .networkPolicy(NetworkPolicy.NO_CACHE)
                    .memoryPolicy(MemoryPolicy.NO_CACHE)
                    .transform(new RoundedTransformation(1000, 0))
                    .resize(500, 500)
                    .centerCrop()
                    .into(pic);
    }

ImagePicker.java is a standard image picker file.

I've also tried to delete the cache from the app with the following function

public static void trimCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
}

Aucun commentaire:

Enregistrer un commentaire