Novoda ImageLoader - loaded same image - android

I'm using Novoda ImageLoader (https://github.com/novoda/ImageLoader) and when I use this loader in CursorAdapter everything is fine. But when I want to use without any Adapter in Activity with one ImageView, I get same image for different URLs.
//onCreate in Activity
imageManager = MyApplication.getImageLoader();
imageTagFactory = new ImageTagFactory(this, R.drawable.ic_refresh_inverse);
imgView = (ImageView) findViewById(R.id.imgIV);
//fillData - when I finish loading img URL from DB with CursorLoader
imgView.setTag(imageTagFactory.build(imgURL));
imageManager.getLoader().load(imgView);
//code in onCreate Application
LoaderSettings settings = new SettingsBuilder().withDisconnectOnEveryCall(true).withCacheManager(new LruBitmapCache(this)).build(this);
imageManager = new ImageManager(this, settings);
When I looked to the cache directory, the different image is loaded, but not used.
Any idea where can be problem or how to properly use this library to simply load only one image?
Thank you!

The issue is probably similar to this https://github.com/novoda/ImageLoader/issues/41
do you have query parameters in the url? if so try to add this
.withEnableQueryInHashGeneration(false) on the setting builder
This is a bug that is inverting the logic behind the parameter enableQueryInHashGeneration. This parameter should enable/disable (by default should be enable) the query part of the url in the generation of the hashname of the image. This is useful when you have session token in the url as a parameter and you don't want to download the same image over and over.
The problem will be solved in the 1.5.6 version

Related

Android OutOfMemory

What is my Problem (KISS)?
After showing the 3rd Image in Detail my App crashes cause OutOfMemory
Background information about my App:
I made an offline Supermarket. All products are shown in a ListView.
All Images I use are stored as String (Uri from every Image) in a sqlite Database by SugarORM.
The ListView element got a ArrayAdapter where the Information for each Item in the ListView is set.
Like this:
ImageView imageView = (ImageView) rowView.findViewById(R.id.productimage);
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
imageView.setImageURI(productList.get(position).getPictureUri());
handler.removeCallbacks(this);
}
});
My Intention was, to load the Images in the Background to Click on it, even before the Image is loaded (But it did'nt work for me :/ The Image is Shown, but I cant click on any other View Element while Loading the Image).
The "getPictureUri" gets the URI from the Picture that the Admin has choosen for the product with the following Code:
Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto,1);
AndroidManifest.xml
android:hardwareAccelerated="false"
android:largeHeap="true"
I extend only from "AppCompatActivity" (Needed for Toolbar). Meaning of this: I don't use "Activity" even once.
Could anyone give me a good advice, how to clear the cached Images (I think this might be the Problem) and besides, maybe one of you got the Time to help me with my "Image Async Loading while you are able to move freely in your app" Problem.
Using setImageUri() is rather intensive on memory, especially when trying to load large bitmaps from a local resource. Instead, use tried and true solutions like Picasso or Glide for any/all image loading tasks. The libraries will work offline, can definitely load images from URIs pointing to a local resource and would most likely solve all your memory issues with image loading.

Setting image from resource in NetworkImageView - Android

I would like to set the image of NetworkImageView from a resource.
I am currently using a configuration in which if there is a picture associated with the content, the image is downloaded from the web. If there is no image, a predefined resource is used.
final NetworkImageView image = (NetworkImageView) dialog.findViewById(R.id.image_quiz);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (noImage)
image.setDefaultImageResId(R.mipmap.splashh);
else {
urlImage = content.getImg();
image.setImageUrl(urlImage, imageLoader);
}
This snippet in contained in a modal and gets called several times.
If there is a sequence of content without an image, the resource is shown correctly.
If there is a sequence of content with images, the images are shown correctly.
THE PROBLEM
If the first content requires an image, the picture is downloaded from the web without any issue.
If the second content does not require an image, the previous image remains and is shown instead of the resource R.mipmap.splashh
Is there any method like setImageResource ?
Looking at the code of NetworkImageView, I saw that it sets SetImageBitmap(null) to clean the currentImage.
You can try to set it before calling setDefaultImageResId but you will let the NetworkImageView in a invalid state internally.
I would recommend to use a more powerful library, like Square's Picasso or Google's Glide

Android, How to download two images efficiently

I've tried lots of different ways to download images and none of them actually worked, i have managed to get something working but its not perfect.
I made a new thread for downloading the bitmaps, im not storing them on the system storage or caching them in the memory for later use. If i keep opening this activity over and over, the ram usage for this app keeps getting higher and higher, and i do not want that at all!
URL url = new URL("http://10.0.0.21:80/1.png");
bitmapOne = BitmapFactory.decodeStream(url.openStream());
URL url2 = new URL("http://10.0.0.21:80/2.png");
bitmapTwo = BitmapFactory.decodeStream(url2.openStream());
runOnUiThread(new Runnable() {
#Override
public void run() {
image.setImageBitmap(bitmapOne);
image2.setImageBitmap(bitmapTwo);
}});
Thank you all for your answers! but...
I would only like to use pure java and no third party libraries for loading images, I was looking for a way to reduce memory usage and not so much of finding another way of downloading them. Im also not sure that having two urls and loading each stream looks very professional as the client could disconnect at any given moment (lets just pretend it does) and it does not load them both which are extremely required!
Use Picasso library, everything will be a breeze:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load("http://SomeUrl/CodeFeature.jpg")
.into(imageView);
A good tutorial can be found here.
https://github.com/zetbaitsu/Compressor
Usage:
compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);
This library maybe helpful for you.
You can visit GitHub. search "Android", sort by: "Most Stars"
, lots of awesome library you will found.
Volley can be a good alternative too, with the ability of displaying animated gifs. This snippet illustrate the basic use, here with image cache, something that can make your app not to be memory and network too demanding:
ImageLoader.ImageCache imageCache = new BitmapLruCache();
ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(getApplicationContext()), imageCache);
NetworkImageView myImage = (NetworkImageView) findViewById(R.id.myImageView);
myImage.setImageUrl(""+mSpeaker.getHeadshotUrl(), imageLoader);
loadBitmap("http://yourdomain.com/yourImage.jpg", myImage);
For this to work all you need is the Volley library and BitmapLruCache.java

Picasso always download from network

Hi I'm trying to understand how to use the Picasso library to cache my downloaded images, so I created a very simple app with one activity, put an ImageView on it and wrote the simplest Picasso line:
Picasso.with(this).load("http://www.estambiente.it/wp-content/uploads/2009/12/Patern_test.jpg")
.placeholder(R.drawable.holder)
.error(R.drawable.error)
.into(im);
but I wanted to see the cache indicators, so I wrote this to show them:
OkHttpClient picassoClient = new OkHttpClient();
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(picassoClient)).build();
picasso.setIndicatorsEnabled(true);
picasso.load("http://www.estambiente.it/wp-content/uploads/2009/12/Patern_test.jpg").into(im);
this code always show the red flag (meaning the image comes from the network) and if I try to open the app while I'm not connected, the error image is shown.
what am I missing here?

Android: Universal image loader overrides old image

The user can upload an image and I want to display the image in a listview. This is the code so far:
final String urlForImage = baseUrlForImage + jsonObject.get("imageName");
new DownloadImageTask(cell.imageViewCustomer).execute(urlForImage);
final DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(cell.imageViewCustomer.getDrawable())
.showImageOnFail(cell.imageViewCustomer.getDrawable())
.showImageOnLoading(cell.imageViewCustomer.getDrawable()).build();
imageLoader.displayImage(urlForImage, cell.imageViewCustomer, options);
But now if the user uploads a new image, the older one from the cache will be loaded in the listview and the new image will be loaded every time from the internet.
How can I override the old image from the user, if the user uploads a new image?
This could help you. Cleaning the cache (or a part of) sounds like a good way.
An easy solution is to change the url of the image, just add ?v=1 at the end and increment the number each time the user changes the image.

Categories

Resources