Improve the Volley NetworkImageView image quality? - android

I'm Using Volley NetworkImageView to load image from web.following method is to get ImageLoader.
public static ImageLoader getVolleyImageLoader(RequestQueue queue){
ImageLoader imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(5);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
return imageLoader;
}
this is layout widget
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/deal_thumb_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="#dimen/deals_image_left_margin" />
this is working fine.load image successfully. but the quality of the image is bad.size of the image is too small than the actual image.
How can I improve the quality of the image?

You have to include the volley source code into your project (as suggested in the Google IO 2014 Video)
Then open ImageLoader from the toolbox folder and change the configuration of the bitmap returning by the ImageRequest :
protected Request<Bitmap> makeImageRequest(String requestUrl, int maxWidth, int maxHeight,
ScaleType scaleType, final String cacheKey) {
return new ImageRequest(requestUrl, new Listener<Bitmap>() {
#Override
public void onResponse(Bitmap response) {
onGetImageSuccess(cacheKey, response);
}
}, maxWidth, maxHeight, scaleType, Config.RGB_565, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
onGetImageError(cacheKey, error);
}
});
}
Just change the Config.RGB_565 by Config.ARGB_8888 and you're done

Related

Issues with loading bitmaps with Picasso library in onPostExecute()

I've got a problem loading bitmaps with Picasso in a loop. I'm loading them in AsyncTasks onPostExecute(). I know that i need global Target variables to prevent them from being collected by GC. Still none of my bitmaps are loaded. Here's my code:
private static Map<Integer, Bitmap> markerBitmaps;
private String[] markerUrls;
private Integer[] catIds;
//some code
private final static List<Target> targets = new ArrayList<Target>();
#Override
protected void onPostExecute(Void result) {
final int[] id = new int[1];
int counter = 0;
for(String s : markerUrls) {
id[0] = catIds[counter];
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
targets.remove(this);
markerBitmaps.put(id[0], bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
targets.remove(this);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Log.i("BITMAP URL IS:", s);
targets.add(target);
Picasso.with(MapsMenuActivity.this)
.load(s)// Start loading the current target
.into(target);
counter++;
}
Log.i("BITMAPS NUM: ", String.valueOf(markerBitmaps.size()));
}
The size of my markerBitmaps map is always 0.

Android Volley ImageLoader return wrong oriented bitmap

I am using volley's image caching through ImageLoader
mImageLoader.get(vehicleInfo.getUserInfo().getProfileImage(), new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
if (imageContainer.getBitmap() != null) {
ivUserImage.setImageBitmap(imageContainer.getBitmap());
}
}
with url
https://vehicollate-dev.s3.ap-south-1.amazonaws.com/uploadVehicles/102/102_PB08BT0402.png
image orientation is portrait but it imageview shows landscape.

Images are not stored in the cache

I have this method, everything is worked perfectly but images always got from server and not load from cache! what happened ?
public static void makeImageRequest(String Unique_ID, final View parentView, final int id) {
String url = FILE_UPLOAD_FOLDER + Unique_ID + ".png";
final int defaultImageResId = R.drawable.user;
// Retrieves an image specified by the URL, displays it in the UI.
ImageCacheManager.getInstance().getImage(url, new ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
ImageView imageView = (ImageView) parentView.findViewById(id);
imageView.setImageResource(defaultImageResId);
}
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
if (response.getBitmap() != null) {
ImageView imageView = (ImageView) parentView.findViewById(id);
imageView.setImageBitmap(response.getBitmap());
} else if (defaultImageResId != 0) {
ImageView imageView = (ImageView) parentView.findViewById(id);
imageView.setImageResource(defaultImageResId);
}
}
});
}
Just use Picasso instead ImageCacheManager. Picasso is a powerful image downloading and caching library for Android. Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Here also can manage whether the image is successfully downloaded or it fails:
Picasso.with(context)
.load("http://i.imgur.com/DvpvklR.png")
.into(imageView, new Callback() {
#Override
public void onSuccess() {
// your code
}
#Override
public void onError() {
// your code
}
});
You should only add this line in your gradle:
compile 'com.squareup.picasso:picasso:2.5.2'
Hope it helps!

Load image for android

I'm loading a picture from the network for an ImageView but the imageView is not immediately showing up.
My code is:
protected void onStart() {
iv_head.setImageDrawable(this.getResources().getDrawable(R.drawable.default_head));
BaseController.getImageForUrl(HttpConstant.SERVER_HEAD + "/" +customer.getExtra().getLogo(), iv_head, R.drawable.default_head);
super.onStart();
}
//获取图片的数据
public static void getImageForUrl(String url, final ImageView imageView, final int errorImage) {
ImageRequest imageRequest = new ImageRequest(
url, new Response.Listener<Bitmap>() {
#Override
public void onResponse(final Bitmap response) {
imageView.setImageBitmap(response);
imageView.invalidate();
}
}, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
imageView.setImageResource(errorImage);
}
});
QueueManager.getRequestQueue().add(imageRequest);
}
I'm sure that it is loading the Bitmap but ImageView is still giving problems.
If I click anywhere on the screen, then ImageView shows. Does anyone know what's happening?
Use
imageView.setImageDrawable(new BitmapDrawable(imageView.getResources(), response));
instead
imageView.setImageBitmap(response);
imageView.invalidate();
I would try to use the Picasso library instead...then your code would be like this:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
nice and simple, no headaches, no reinvention of the wheel. Good luck!

ImageView refresh with Glide

I have one ImageView and one image loaded in it with Glide:
Glide.with(ImageView.getContext())
.load(url)
.dontAnimate()
.placeholder(R.drawable.placeholder)
.signature(stringSignature)
.into(new GlideDrawableImageViewTarget(ImageView) {
#Override
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
progressBar.setVisibility(View.GONE);
}
});
and when I want refresh the image, I run this same code again only with new signature. It's working perfectly, but when new loading is started, the visible image is gone immediately.
Question
Is possible keep the image in ImageView and replace it after new image is downloaded?
That's the expected behavior.
Each time you call .load(x), Glide call .clear() on the target and its associated request.
That's how Glide is able to handle its pool of Bitmaps, otherwise it would have no way to know when to recycle a Bitmap.
In order to implement this, you need to switch between two Targets, here is the core idea :
public <T> void loadNextImage(#NonNull T model,
#NonNull BitmapTransformation... transformations) {
//noinspection MagicNumber
int hash = model.hashCode() + 31 * Arrays.hashCode(transformations);
if (mLastLoadHash == hash) return;
Glide.with(mContext).load(model).asBitmap().transform(transformations).into(mCurrentTarget);
mLastLoadHash = hash;
}
Target mCurrentTarget;
private class DiaporamaViewTarget extends ViewTarget<ImageView, Bitmap> {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
mLoadedDrawable = new BitmapDrawable(mImageView.getResources(), resource);
// display the loaded image
mCurrentTarget = mPreviousTarget;
You can set loaded Drawable as placeholder in next loading, like this:
private Drawable placeholder = ContextCompat.getDrawable(ctx, R.drawable.placeholder);
public void loadImage(String url, ImageView imageView) {
Glide.with(imageView.getContext())
.load(url)
.placeholder(placeholder)
.into(new GlideDrawableImageViewTarget(imageView) {
#Override
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
placeholder = drawable;
}
});
}

Categories

Resources