Rektirino
Rektirino

Reputation: 582

App crashing due to high resolution image

In my app, I'm giving user the feature to set an image for the app's homescreen background(from gallery). The app works fine if the image if low resolution but if the user sets a high resolution image the app crashes on startup giving this error.

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@35f21ac is not valid; is your activity running?

I used the override method of glide to reduce the resolution of the image:

Glide.with(MainActivity.this).asBitmap()
    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE).dontAnimate())
    .load(storageUtil.getWallpaperStatus())
    .apply(new RequestOptions().override(320,640).centerCrop())
    .into(viewPagerBackground);

Now, the crash frequency has reduced, but still sometimes the app crashes.

What could be the best solution for this problem?

Upvotes: 3

Views: 923

Answers (1)

Nouman Ch
Nouman Ch

Reputation: 4121

paste your code within this code

if(!((Activity) context).isFinishing())
{
    //your code 
Glide.with(MainActivity.this).asBitmap()
                    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE).dontAnimate())
                    .load(storageUtil.getWallpaperStatus())
                    .apply(new RequestOptions().override(320,640).centerCrop())
                    .into(viewPagerBackground);
}

This error occur sometime when we are loading image using glide but our activity is destroyed already.

Upvotes: 0

Related Questions