java Code for load image fom URL
ImageView img = (ImageView) findViewById(R.id.imageView);
Glide.with(MainActivity.this)
.load("https://yildirim.bel.tr/uploads/haberler/4509c5f0-7498-
4932-85d4-767e48408299image.jpeg")
.into(img);
manifest
<uses-permission android:name="com.googleandroid.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Gradle
implementation 'com.github.bumptech.glide:glide:3.8.0'
First create a class like that:
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
// new since Glide v4
#GlideModule
public final class MyAppGlideModule extends AppGlideModule {
// leave empty for now
}
Then call the Glide as:
GlideApp.with(context)
.load("https://yildirim.bel.tr/uploads/haberler/4509c5f0-7498-
4932-85d4-767e48408299image.jpeg")
.override(300, 200)
.into(img);
Try this
RequestOptions options = new RequestOptions()
.centerCrop()
.placeholder(R.mipmap.ic_launcher_round)
.error(R.mipmap.ic_launcher_round);
Glide.with(this).load(image_url).apply(options).into(imageView);
Related
I have this line of code on my android studio app
if (obj.getPhotoUrl() != null) {
ImageUtil.loadImage(GlideApp.with(context), obj.getPhotoUrl(), avatarImageView);
}
But it shows error "
Required type:
GlideRequests
Provided:
com.devlomi.fireapp.utils.glide.GlideRequests "
This is my MyAppGLideModule class
#GlideModule
public class MyAppGlideModule extends AppGlideModule {
#Override
public void registerComponents(Context context, Glide glide, Registry registry) {
registry.prepend(String.class, ByteBuffer.class, new Base64ModelLoaderFactory());
}
}
How to correct the error I want to load images using the Glide
if all you want is to load an image using Glide into an ImageView, then you don't need to extend any class inorder to use Glide.
you can just load the the image using its Uri with this line of code :
Glide.with(context).load(obj.getPhotoUrl()).into(avatarImageView);
if that's not what you meant in your question, can you explain more ?
With Glide Library it becomes a little easy to load the images from a url into an ImageView
And for this we use
Glide.with(getApplicationContext).load.(obj.getPhotoUrl()).into(imageView)
I'm trying to load an image using Glide.
I gave it a url however it results in a FileNotFound exception.
However this is not my concern, my concern is that I want to load a placeholder in case any of these errors occur.
I followed the guide to use glide, but nothing occurs.
Please take a look at my code.
I tried adding RequestListener and error fallback.
Glide.with(imageView.getContext()).load(url)
.transition(DrawableTransitionOptions.withCrossFade())
.apply(new RequestOptions().placeholder(placeholder))
.error(Glide.with(imageView.getContext()).load(placeholder))
.listener(new RequestListener<Drawable>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(imageView);
I want my placeholder to be loading in my imageview.
Actual result is that my imageview is loaded with some placeholder not defined by me, something indicating that the image link is forbidden or something.
Expected: I want my placeholder to be loaded.
Try this:
int placeholder = R.drawable.error
GlideApp.with(imageView.getContext())
.load(url)
.apply(new RequestOptions().placeholder(placeholder)
.error(placeholder).dontTransform()
.diskCacheStrategy(DiskCacheStrategy.ALL))
.into(mImageView);
You can refer code below to show placeholder image in case of Error :
Glide.with(context).load(BASE_URL + imageUrl)
.apply(new RequestOptions().placeholder(R.drawable.defaultimage)
.error(R.drawable.defaultimage)).into(imageView);
Here is modified code:
int placeholder = R.drawable.placeholder
Glide.with(imageView.getContext())
.load(url)
.error(placeholder)
.placeholder(placeholder)
.into(imageView);
Try with this code below.
Glide.with(your context).load(imageurl).apply(RequestOptions.placeholderOf(your drawable here)).error(your error drawable).into(your imageview);
Hope it works...
Try using this-
.error(getResourses.getDrawable(R.drawable.placeholder))
instead of using this-
.error(Glide.with(imageView.getContext()).load(placeholder))
Try using RequestOptions
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_placeholder);
requestOptions.error(R.drawable.ic_error);
Glide.with(context)
.load(url)
.apply(requestOptions)
.into(holder.imageView);
I am showing list of images from server. Images may have different size and resolutions. I have observed that Image is taking time to load in Imageview.
Even in good internet network, its taking much time.
if (imageView != null && imageUri != null) {
RequestOptions options = new RequestOptions();
options.error(R.drawable.ic_payment_failed);
options.format(DecodeFormat.PREFER_RGB_565);
options.override(500, 500);
imageView.setImageUri(imageUri);
Glide.with(context).asBitmap()
.apply(options)
.load(imageUri)
.into(imageView.getProfilePic());
}
}
I have tried lot many glide methods to improve loading speed, but no result.
And if try to load large image i.e 4-5 MB, Glide throwing Unable to load resource exception. To deal with this type of issue i have used transformation. But still no result.
I want image should display fast.
you can Use GlideApp for new version glide.
you must create a java class with this name MyAppGlideModule in your project. and make class like below code.
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
#GlideModule
public final class MyAppGlideModule extends AppGlideModule {
}
and you can also use OKHttp for glide with add this dependence in your Gradle.
implementation "com.github.bumptech.glide:okhttp3-integration:4.8.0"
and replace that class
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;
#GlideModule
public final class MyAppGlideModule extends AppGlideModule {
#Override
public void registerComponents(Context context, Glide glide, Registry registry) {
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(15, TimeUnit.SECONDS)
.connectTimeout(15, TimeUnit.SECONDS)
.build();
OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);
glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
}
}
after that Build your project.after you Build project. you can Use GlideApp instead Glide.like this
GlideApp.with(context)
.load(myUrl)
.placeholder(placeholder)
.into(yourTarget);
you can use the latest version of glide.
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
When I use Glide to put and image inside an ImageView and then I "zoom in" (with a function that I bound to my ImageView), I see that the image has a low quality.
I already read Glide documentation but I'm not able to solve the problem.
Note: I use Glide inside a ViewPager.
This is what I did following the documentation
Glide
.with(context)
.loadFromMediaStore(uri)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
This is the Manifest.xml
<manifest ...>
<application...>
<meta-data android:name="come.package.glide.MyGlideConfiguration"
android:value="GlideModule"/>
</application>
</manifest >
This is my GlideConfiguration
public class GlideConfiguration implements GlideModule {
#Override
public void applyOptions(Context context, GlideBuilder builder) {
// Apply options to the builder here.
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
#Override
public void registerComponents(Context context, Glide glide) {
// register ModelLoaders here.
}
}
I also tried doing just
Glide
.with(context)
.loadFromMediaStore(uri).asBitmap().format(DecodeFormat.ALWAYS_ARGB_8888)
But the quality is always low.
Does anybody see where is my error??
Instead of calling
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
try
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
I know it isn't very practical to load bitmaps from the device storage synchronously, but I really have to do it. I haven't figured out any way to do this.
Yes is possible and is in glide documentation.
For example if you need to retrive the Bitmap synchronously you can do:
Glide V3:
Bitmap myBitmap = Glide.with(applicationContext)
.load(yourUrl)
.asBitmap()
.into(500, 500)
.get()
Glide v4:
FutureTarget<Bitmap> futureBitmap = Glide.with(applicationContext)
.asBitmap()
.load(yourURL))
.submit();
Bitmap myBitmap = futureBitmap.get();
Note: This code need to be run in the background or the app will crash.
Using interfaces to load data after image is loaded is a better option.
Create a new file called OnglideLoaded.java file like this:
import android.graphics.drawable.Drawable;
public interface OnGlideLoaded {
void onGlideLoaded(Drawable drawable);
}
Now create setter method for it like this where glide is:
public void setOnGlideLoaded(OnGlideLoaded onGlideLoaded) {
this.onGlideLoaded = onGlideLoaded;
}
Not call it inside Glide Target:
Glide.with(context)
.asBitmap()
.load(uri)
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
icon = resource;
if (onGlideLoaded!=null){
//This will send the image to every registered interface
onGlideLoaded.onGlideLoaded(icon);
}
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
Now call the interface like this where you want the image:
reference.setOnGlideLoaded(new OnGlideLoaded() {
#Override
public void onGlideLoaded(Drawable drawable) {
imageView.setImageBitmap(drawable);
}
});
Here, reference will be the object where you created the setter method.