Android studio glide issue - android

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)

Related

android picasso .into(new Target() causing method error

We are using the following code to load images into notification .setLargeicon parameter in our android webview app. But unfortunately .into(new Target() { displays the following warning:
Class 'Anonymous class derived from Target' must either be declared abstract or implement abstract method 'value()' in 'Target'. We are at loss as what we should do to solve this issue.
Picasso.get().load("https://mbracecloud.com/appln_enterprise/images/reconnect_feature7.jpg")
.into(new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
notificationBuilder1.setLargeIcon(bitmap);
notificationManager.notify(notification_id, notificationBuilder1.build());
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
// Do nothing
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
// Do nothing
}
});
Update:
Based on inputs given below, we put the following import:
import com.squareup.picasso.Target;
and tried to load image, but we are encountering a new error:
Class 'Anonymous class derived from Target' must either be declared abstract or implement abstract method 'onBitmapFailed(Exception, Drawable)' in 'Target'
Please help us out on this issue.
Guessing you're importing the wrong Target and specifically the annotation one. Change
import java.lang.annotation.Target;
to
import com.squareup.picasso.Target;
Based on your error onBitmapFailed(Exception, Drawable), you are overriding this method with only Drawable parameter but the Target class wants to override abstract method with Exception and Drawable parameters. So, try replacing onBitmapFailed with the implementation below:
#Override
public void onBitmapFailed(final Exception exception, final Drawable errorDrawable) {
// Do nothing
}

Image loading using Glide taking too much time

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'

Low quality with Glide

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)

Android + Volley: how to get image bitmap using ImageLoader?

I'm trying to get images for list view using Volley Library. I created simple HTTP helper with the following method.
/**
* Processing Image request and gets the image with given URL
*/
public Bitmap makeImageRequest(String url) {
ImageLoader il = new ImageLoader(queue, new BitmapLruCache());
il.get(url, new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
mBitmap = processImageResponse(response);
}
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(Constants.Global.ERROR, "Error: " + error.getMessage());
mBitmap = null;
}
});
return mBitmap;
}
But problem is that:
new BitmapLruCache()
Method is not recognized.
So i tried to create ImageLoader using the following code which i found on the URL:
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
But in thos code i cannot find out where to get
AppController
Because the method code is triggered from the custom
public class HttpHelperClass
And called from the activity using the:
// Try to load remote image from URL
Bitmap bm = http.makeImageRequest("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png");
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bm);
Is it the right approach how to load images and how can i repair my code to make succcessfull request?
Many thanks for any advice.
I think your makeImageRequest method will always return null because it takes some time for the onResponse or onErrorResponse listeners to get called but you return the mBitmap immidately!
If you want to use Volley's ImageLoader you'd better get image inside your activity or ... not from another class like your HttpHelperClass.
Also AppController is a class that extends Application and you should create it yourself. (It's in your AndroidHive link. Section 3. Creating Volley Singleton class)
And also for caching images you should not create a new ImageLoader everytime because in this way the caching gets meaningless. You should get that from your AppController class.
Furthermore I suggest you using Picasso because it's way better that Volley in image loading and a lot easier!
With Picasso you only need to call the following line to load an image from web into an ImageView:
Picasso.with(context).load(urlString).to(imageView);

Set background resource using Picasso

I know Picasso is an awesome library to play with images.
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
with this code i can load an image to an image view.
But is it possible to set a background resource , using Picasso ?
The Javadoc for Picasso's RequestCreator class has the following example:
public class ProfileView extends FrameLayout implements Target {
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
setBackgroundDrawable(new BitmapDrawable(bitmap));
}
#Override public void onBitmapFailed() {
setBackgroundResource(R.drawable.profile_error);
}
}
I just had a work around with the Picasso library, I was attempting to set the image as a background as well.
Picasso library made it very easy to do this, there is method by name "FIT()" which will do this job for you.
The one magic line from Picasso is
Picasso.with(context).load(mImageURLS.get(position))
.fit().placeholder(R.drawable.rtrt).into(mImageDownloader);
.fit() does the trick, thanks.

Categories

Resources