简体   繁体   中英

Loading Image in Image View using Glide

I am working on an app which show an image which is loaded from my firebase database. I used Glide for that purpose.

I am using a button, on the click of that button a new activity will open which shows the image fetched from the url. The image load perfectly if url loaded correctly but if I cancelled in between, the app is crashing. Why it is happening and also, Is there any option to save the image in cache using glide, so that It can load it once and show without fetching it from firebase.

Here is my code to display image from url:

public void showImage(){
    mStorageRef.child("TimeTable").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            url = uri.toString();
            RequestOptions options = new RequestOptions().centerCrop().placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher_round);
            Toast.makeText(TimeTable.this, uri.toString(),Toast.LENGTH_SHORT).show();
            Glide.with(TimeTable.this).load(url).apply(options).into(image);
        }
    });
}

Here is the Error log, when I closed the activity before image loaded completely..

在此处输入图片说明

Please ask if any further details are required.

You can cache your images loading using Glide. I am referring to the tutorial here where you can learn the basics of image caching using Glide.

Now about the crashing problem, it clearly says that you are trying to load something from an activity which is already destroyed. As you have a firebase listener added where you are loading the image, you need to destroy the listener as you are leaving the activity.

You might consider using onPause , onDestroy functions to handle the remove listener action. Check the removeEventListener function of Firebase.

However, I would recommend using activity scoped versions of the loaders so that Firebase task takes care of these things by itself. Please see the answer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM