'How to use Glide library in android to display image from url? Glide Usage

EDIT

If I try to use glide, and import an image ( https://mega.nz/#!nJ8yECaR!wHK3fz1xPEx9VMjTPuxzao6XDemAXy0T5s7KBUCsffs) the image button goes to the all screen, while the picture is the right position. And if I "click", the button see in all the screen.Anyone can tell me the right method step-by-step?



Solution 1:[1]

Your problem is that you are running out of memory, you need to load the images into cache. Picasso and glide are great libraries for handling images.

With glide you will add the image like this:

GlideApp
.with(MyActivity)
.load(R.drawable.updatelog)
.centerCrop()
.into(myImageButton);

and with picasso you would load it like this:

Picasso.with(context)
.load(R.drawable.updatelog)
.fit().centerCrop()
.into(myImageButton);

As @Nilu stated - the png is not loaded in a ImageView but you are still using png/images that uses memory to load.

That is why you are able to load 1 image but crashes when you load more images/png's because your application is running out of memory as your log cat suggest:

Caused by: java.lang.OutOfMemoryError: Failed to allocate a 74649612 byte allocation with 16777120 free bytes and 56MB until OOM

I would suggest using a ImageButton then loading it with picasso or glide.

Solution 2:[2]

Why not just use an ImageButton. It is the preferred way.

Solution 3:[3]

Try to convert .PNG file to webp format by importing same image in Android Studio For more details visit https://developer.android.com/studio/write/convert-webp.html

Solution 4:[4]

you may can't use alpha in background image so that may cause the problem .

Solution 5:[5]

Library Setup

Add to your app/build.gradle file:

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.12.0'
  // Glide v4 uses this new annotation processor -- see https://bumptech.github.io/glide/doc/generatedapi.html
  annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

Make sure to create a MyAppGlideModule that simply extends from AppGlideModule and has the @GlideModule annotation.

import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule

// new since Glide v4
@GlideModule
class MyAppGlideModule : AppGlideModule() { 
    
}

Usage

Glide.with(context)
    .load("URL")
    .into(ivImg)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AskNilesh
Solution 3 Nikhil Lotke
Solution 4
Solution 5 Quick learner