UrlImageViewHelper resize image with ImageView size - java

I'm using UrlImageViewHelper, I would like to use less memory. So I thought about resizing image downloaded and fit with imageView size (more or less small depending on screen). Is it possible?
Here is my code :
UrlImageViewHelper.setUrlDrawable(mImageReport, mCurrentReportString.getUrlImages());

U can use [ https://github.com/square/picasso ] to load and cache, resize Image from Image Url .
Picasso.with(getActivity())
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(screenWidth, imageHeight)
.centerInside()
.into(imageView);

Related

Android - I am having problems with adding more than 8 imageviews to RelativeLayout

enter ?
I need to add 24 images to RelativeLayout and I first tried it with XML,
which just shut down the app after I added 8th ImageView.
So I tried with coding like above, and it shows me that message.
It says OutOfMemory, failed to allocate ___ to a memory with 6mb OOM and I don't really know what it means but I am guessing image files are too large?
It caused error like that when I just tried with XML too.
enter image description here
but the entire set of images are less than 100kb though.
How can I deal with this issue?
looks like you have drawables in drawable folder. So if you have really large-density screen, it scales. You should create scaled images for all dpi's or init images manually:
Options options = new Options();
options.inScaled = false;
bitmapImage = BitmapFactory.decodeResource(context.getResources(),
R.drawable.imageName, options);
imageview.setImageBitmap(bitmapImage);
or
InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);
imageview.setImageBitmap(originalBitmap);
Use Picssso library (http://square.github.io/picasso/). It has fit() method that is very useful for loading many images. Example:
Picssso.with(context).load(R.drawable.yourimage).fit().into(yourImageView);

Glide not updating image android of same url?

I have the same url for an image. When I update this image more than one time it shows the previous image. The image and picture version on the server is updated but Glide is not showing the new image.I want to get new image every time and cache it .
Glide.with(context)
.load(Constants.COPY_LINK_BASE_URL + info.getDisplayPicture())
.placeholder(R.drawable.ic_profile).dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.signature(new (SettingManager.getUserPictureVersion(context)))
.into(ivUserProfilePhoto);
I can reproduce this bug by changing internet ,on one internet its change image as expected on other internet it remain same after 2 or 3 tries to changing image
//Use bellow code, it work for me.Set skip Memory Cache to true. it will load the image every time.
Glide.with(Activity.this)
.load(theImagePath)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(myImageViewPhoto);
Glide 4.x
Glide.with(context)
.load(imageUrl)
.apply(RequestOptions.skipMemoryCacheOf(true))
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.into(imageView);
Glide 3.x
Glide.with(context)
.load(imageUrl)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView);
TL;DR.
you can skip caching by adding the following lines:
GLIDE v4
Glide.with(context)
.load(url)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.apply(RequestOptions.skipMemoryCacheOf(true))
.into(imageView);
GLIDE v3
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(imageView);
OR : you can workaround caching by adding a dummy random argument to your URL:
Glide.with(context)
.load(url + "?rand=" + (new Random().nextInt()))
.into(imageView);
What's happening?
When your picture is loaded the first time, it's stored locally in what's called a cached memory (or simply "a cache"). When you request it for a second time Glide fetches if from the cache as if it was a successful request. This is meant for many good reasons such as: offloading your server, saving your users some data and responding quickly (offering your users a smooth experience).
What to do?
Now, concerning your issue: you need to disable the cache in order to force Glide to fetch your image remotely every time you ask for it. You can do the following:
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.signature(imageVersion) // supposing that each new image has its own version number
.into(imageView);
Or, in the case where you can't know when a picture is changes (no imageVersion), use a unique signature for each picture.
Glide.with(context)
.load(url)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.into(imageView);
Another clean way would be to configure your picture's cache strategy in your server and use .diskCacheStrategy(DiskCacheStrategy.SOURCE).
A hacky trick worth mentioning
If adding a dummy GET paramter to your target URL doesn't break anything, then you could simply workaround caching by passing a random argument each time you fetch the image, this will push Glide to think you're querying a new endpoint, since the image cannot possibly exist in the cache.
Glide.with(context)
.load(url + "?rand=" + (new Random().nextInt())) // "&rand=" if your url already has some GET params
.into(imageView);
Glide caching API here.
I my case Glide not recognize .signature(), so additionally to #MGDavies's answer you maybe need something like this
// in kotlin
Glide.with(context)
.load("url")
//using apply to RequestOptions instead signature directly
.apply(RequestOptions().signature(ObjectKey("time that image was updated")))
.into(thumb)
This is an old question, so I'm sure you've got your fix by now. However I believe both of the big libraries(Picasso and Glide) now support something similar.
Glide has its signature API you may wish to look into:
https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
Glide.with(yourFragment)
.load(yourFileDataModel)
.signature(new StringSignature(yourVersionMetadata))
.into(yourImageView);
I've not had a chance to work with this myself, but it looks as though you need to make a call to your API to get the version Metadata, then a subsequent call to fulfil it.
Alternatively, look into ETags.
Good luck!
Glide 4.8.0
Glide.with(mContext).asBitmap()
.load(nopicUrl)
.apply(RequestOptions.skipMemoryCacheOf(true))
.apply(RequestOptions.signatureOf(new ObjectKey(String.valueOf(System.currentTimeMillis()))))
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.DATA))
.into(ivImageView);
Now Glide latest version, you have to use RequestOption for clear cache.
RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true);
Or for everytime load picture you have to use a String in as signature.
RequestOptions options = new RequestOptions()
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())));
Finally:
Glide.with(CompressSingleActivity.this)
.applyDefaultRequestOptions(options)
.load(currentFile)
.into(currentImageView);
When new images are loaded or updated, use below to clear Glide memory and cache :
|==| Clear Glide memory
Glide.get(getApplicationContext()).clearMemory();
|==| Clear Glide Cache()
void clearGlideDiskCache()
{
new Thread(new Runnable()
{
#Override
public void run()
{
Glide.get(getApplicationContext()).clearDiskCache();
}
}).start();
}
diskCacheStrategy and skipMemoryCache did not work for my case.
I was writing to a local image file between Glide requests with the same url. My hack of a solution was to switch between adding a 0/1 to the end of the filename.
Forcing it to change image source every time seems to work.
Try using RequestOptions:
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_placeholder);
requestOptions.error(R.drawable.ic_error);
Glide.with(context)
.setDefaultRequestOptions(requestOptions)
.load(url).into(holder.imageView);
If .setDefaultRequestOptions(requestOptions) does not work, use .apply(requestOptions):
Glide.with(MainActivity.this)
.load(url)
.apply(requestOptions)
.into(imageview);
// or this
Glide.with(MainActivity.this)
.load(url)
.apply(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
.into(imageview);
// or this
Glide.with(MainActivity.this)
.load(url)
.apply(RequestOptions.placeholderOf(R.drawable.booked_circle).error(R.drawable.))
.into(imageview);
it's like the issue is related to Glide Library itself, I found the trick that can fix this, just put a random number after your image URL as a query like an Example code below, it fixed my problem I hope it helps you too
Random random = new Random();
int randomInt = random.nextInt();
Glide.with(context)
.applyDefaultRequestOptions(new RequestOptions()
.circleCrop().diskCacheStrategy(DiskCacheStrategy.NONE))
.load(ConstantVars.BaseUrl + userModel.getAvatarURL() + "?" + randomInt)
.apply(new RequestOptions().error(R.drawable.himart_place_holder))
.into(imageViewProfile);
it will make Glide to reload image anytime you request like there's no cache at all to read from.

Getting image from Webview to ImageView

I have a custom WebView. Inside it I've got an image. The WebView is zoomable. I want to reset zoom when I click a button, extract image from there and place inside ImageView.
So far, with the help of x-code I've managed to write this in onClickListener:
while (wv2.zoomOut()){
wv2.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(wv2.getDrawingCache());
bitmapPath = bitmap;
wv2.setDrawingCacheEnabled(false);
new Thread(new Runnable() {
public void run() {
SaveImage(bitmap);
}
}).start();
tstImage.setImageBitmap(bitmap);
}
The WebView zooms out completely, but the Bitmap I get in ImageView is slightly zoomed in. I need to click 1 more time to get the whole bitmap inside the ImageView
I would say that the Javadoc of WebView.getDrawingCache() method explains your problem.
public Bitmap getDrawingCache (boolean autoScale)
...
Note about auto scaling in compatibility mode: When auto scaling is
not enabled, this method will create a bitmap of the same size as this
view. Because this bitmap will be drawn scaled by the parent
ViewGroup, the result on screen might show scaling artifacts. To avoid
such artifacts, you should call this method by setting the auto
scaling to true. Doing so, however, will generate a bitmap of a
different size than the view. This implies that your application must
be able to handle this size.
In your code you simply have to make this change
bitmap = Bitmap.createBitmap(wv2.getDrawingCache(true));

Android - Picasso 2.5.2 - display watermark

I've been trying to display a watermark on the images loaded with Picasso. I found this: Picasso - transformation bitmap quality , which is interesting to load the overlaid image using the .transform property, but I cannot find the BitmapTransformations.OverlayTransformation library anywhere.
For context sake, I'm loading a GridView and populating it with Picasso, like this:
MainActivity:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
On ImageAdapter:
Picasso picasso = Picasso.with(mContext);
picasso.load(foto.getURL())
.placeholder(R.raw.place_holder)
.error(R.raw.big_problem)
.resize(150, 150)
.centerCrop()
.into(imageView);
On layout XML:
the GridView is within a RelativeLayout
I'd appreciate any help! Thanks
ps: this is how it's used on the link:
Picasso.with(context)
.load(item.getPicture())
.transform(new BitmapTransformations.OverlayTransformation(
context.getResources(), R.drawable.ic_play_video))
.error(R.drawable.picture_placeholder)
.into(target);
Try this for adding watermark text to your images with a picasso transformation:
https://gist.github.com/odedhb/4c72b1d73ddd7ece1949

Display images from Drawable folder using Universal Image Loader

I want to load image from Drawable folder using Universal Image Loader (NOSTRA my code is below
imgLoader = ImageLoader.getInstance();
imgLoader.init(new ImageLoaderConfiguration.Builder(this).build());
Now i am loading image in wallImage (ImageView) below is my code
imgLoader.displayImage("drawable://" + result, wallImage);
my problem is, it is taking to much time to load image. Can anybody give me solution what should I have to do ?
Imgloader.displayImage("drawable://" + result, wallImage);
this method is not recommending for loading the images in drawable... You should use native method as Vigbyor as suggested
imageview.setImageResources(res id);

Categories