bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.65k stars 6.12k forks source link

How Clear image cache or data When upload image #2627

Closed junoprima closed 6 years ago

junoprima commented 6 years ago

I'm new for Glide i need to know how clear old picture when i update or other update profile picture

like in listview i load image with glide in firebase first time install app can load true and then i update my profile picture and updated i back to listview and show old profile pic but if i clear memory my app or delete and reinstall my app show the correct image

this code my list to retrive data

` mOption = new FirebaseRecyclerOptions.Builder(). setQuery(mQuery ,Mainboard.class) .setLifecycleOwner(this) .build();

    mAdapter = new FirebaseRecyclerAdapter<Mainboard, MainboardViewHolder>(mOption) {

        @Override
        public MainboardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mainboard_list , parent , false);
            return new MainboardViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(final MainboardViewHolder holder, int position, final Mainboard mainboard) {
            final DatabaseReference postRef = getRef(position);

            String mb_id = mainboard.mb_id;
            mRef.child( mb_id + "/mb_auther").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() != null){
                        String mb_author = dataSnapshot.getValue().toString();
                        mReference.child(mb_author + "/user_fname").addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String user_fname = dataSnapshot.getValue().toString();
                                Log.e("user_fname" , dataSnapshot.getValue().toString());
                                holder.setAuthor(user_fname);

                                author_post = holder.setAuthor(user_fname);

                                imgRef = sRef.child("profile").child( mainboard.getMb_auther());

                                Glide.with(getContext())
                                        .load(imgRef)
                                        .transition(DrawableTransitionOptions.withCrossFade())
                                        .apply(new RequestOptions().placeholder(R.drawable.pic_profile))
                                        .into(holder.profile);

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                    }
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }

            });

            String dateCreate = mainboard.mb_create;

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("เมื่อ dd MMMM yyyy | HH:mm:ss น.",new Locale("th" , "TH"));

            try {
                Date date = dateFormat.parse(dateCreate);

                String date_create = dateFormat1.format(date);

                Log.d("Date" , date_create);

                holder.setDate(date_create);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            holder.title.setText(mainboard.getMb_title());
            holder.body.setText(mainboard.getMb_body());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), MainboardDetailActivity.class);
                    intent.putExtra(MainboardDetailActivity.MAINBOARD_POST_KEY, postRef.getKey());

                    startActivity(intent);
                }
            });

        }
    };

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
}`

and this code my update profile class

`edit_profile_pic = view.findViewById(R.id.edit_image_profile); edit_profile_pic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, RESULT_LOAD_IMAGE); } });

    edit = view.findViewById(R.id.edit_btn);
    edit.setOnClickListener(this);

    save = view.findViewById(R.id.save_edit_btn);
    save.setOnClickListener(this);

    showProfile = view.findViewById(R.id.show_profile);
    editProfile = view.findViewById(R.id.edit_profile);

    profile_pic = view.findViewById(R.id.edit_profile_pic);

    //get firebase auth instance
    mauth = FirebaseAuth.getInstance().getInstance();
    database = FirebaseDatabase.getInstance();
    DatabaseReference users = database.getReference("user");

    mAuth = FirebaseAuth.getInstance();
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference pathRef = storageRef.child("profile");
    picRef = pathRef.child(String.format(mAuth.getCurrentUser().getUid()));

    user = mauth.getCurrentUser();
    uid = user.getUid();

    true_num.setText(user.getPhoneNumber());
    editTruenum.setText(user.getPhoneNumber());

    final Query query = users.orderByChild("user_id").equalTo(uid);
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                User mUser = dataSnapshot.getValue(User.class);

                //ShowProfile
                fname.setText(snapshot.child("user_fname").getValue().toString());
                lname.setText(snapshot.child("user_lname").getValue().toString());
                mail.setText(snapshot.child("user_email").getValue().toString());
                line.setText(snapshot.child("user_line").getValue().toString());
                face.setText(snapshot.child("user_facebook").getValue().toString());

                //EditProfile
                firstName.setText(snapshot.child("user_fname").getValue().toString());
                lastName.setText(snapshot.child("user_lname").getValue().toString());
                editFace.setText(snapshot.child("user_facebook").getValue().toString());

                editMail.setText(snapshot.child("user_email").getValue().toString() , TextView.BufferType.EDITABLE);
                editLine.setText(snapshot.child("user_line").getValue().toString() , TextView.BufferType.EDITABLE);

                Picasso.with(profile_pic.getContext())
                        .load(path_image + uid + "?alt=media")
                        .placeholder(R.drawable.pic_profile)
                        .into(profile_pic);

                Picasso.with(edit_profile_pic.getContext())
                        .load(path_image + uid + "?alt=media")
                        .placeholder(R.drawable.pic_profile)
                        .into(edit_profile_pic);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return view;
}

@Override
public void onClick(View view) {
    switch (view.getId())
    {
        case R.id.edit_btn:
            editProfile.setVisibility(View.VISIBLE);
            showProfile.setVisibility(View.GONE);
            break;

        case R.id.save_edit_btn:
            showProfile.setVisibility(View.VISIBLE);
            editProfile();
            break;
    }
}

private void editProfile() {

    String edit_email = editMail.getText().toString();
    String edit_line = editLine.getText().toString();

    database.getReference("user").child(uid).child("user_line").setValue(edit_line);
    database.getReference("user").child(uid ).child("user_email").setValue(edit_email);

    EditAndUploadImage();

}

private void EditAndUploadImage() {
    ProfileHelper.showDialog(getContext());
    edit_profile_pic.setDrawingCacheEnabled(true);
    edit_profile_pic.buildDrawingCache();
    Bitmap bitmap = edit_profile_pic.getDrawingCache();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] data = baos.toByteArray();

    UploadTask uploadTask = picRef.putBytes(data);
    uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            ProfileHelper.dismissDialog();
            Uri downloadUri = taskSnapshot.getDownloadUrl();

            Picasso.with(getContext()).load(downloadUri).placeholder(R.drawable.pic_profile).into(profile_pic);
            editProfile.setVisibility(View.GONE);

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            ProfileHelper.dismissDialog();
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK){
        if (requestCode == RESULT_LOAD_IMAGE){

            final Uri imageUri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), imageUri);
                RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
                imageDrawable.setCircular(true);
                edit_profile_pic.setImageDrawable(imageDrawable);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}`
sjudd commented 6 years ago

I'd assume the model is the same for both images? If so, you probably want .signature() with a Key that's unique to each image (an image id, incrementing integer etc). For more detail, see http://bumptech.github.io/glide/doc/caching.html#cache-invalidation.

junoprima commented 6 years ago

what is a key in signature i dont understand it

sjudd commented 6 years ago

There are code samples, javadoc links, and a detailed explanation at the link I provided. Did you get the chance to take a look at that?

stale[bot] commented 6 years ago

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.