Android gms ImageManager never loads image - android

I'm attempting to draw an icon for an achievement using Google Play Game Services API.
However, it is silently failing.
Approach/Issue:
The URI for the image is retrieved successfully, exists and is valid.
I use ImageManager.loadImage to get the image, with an OnImageLoadedListener for callback (com.google.android.gms.common.images.ImageManager).
However OnImageLoadedListener's method, onImageLoaded, is never called.
No error's, no evidence, just completely ignored, I even waited 10 minutes just in case.
Code:
// Get URI [is valid, exists, is of type png, I checked]
Uri uri = getAchievementIconUri(id);
// Use ImageManager to get the icon image
ImageManager.create(context).
loadImage(new ImageManager.OnImageLoadedListener() {
#Override
public void onImageLoaded(Uri u, Drawable d, boolean r) {
/*
* This code is never reached, no call to onImageLoaded is made!
*/
}
}, uri);

It's probably a very late answer, but it might help other people experiencing the same. From the latest google documentation, https://developers.google.com/android/reference/com/google/android/gms/common/images/ImageManager.html#public-methods
Note that you should hold a reference to the listener provided until the callback is complete. For this reason, the use of anonymous implementations is discouraged.
This might explain the behaviour you experienced, as in the first code snippet provided, no reference to the Listener was retained.

Well, for what it matters, I never solved the problem entirely.
It seems that it actually displays the icons occasionally, whenever it feels like it.
I implemented a workaround that loads a compressed, cached version of the image and later replaces it with the ImageManager provided image if it can be bothered doing so.
If anybody works out the correct answer then I will remove this workaround, but until then its here for anybody with the same issue.
Code that loads the local icons:
public Bitmap loadDefaultAchievementIcon(final String id) {
// Get drawable ID for achievement
// iconIdMap is a map of achievement ID strings against their drawable res IDs
int resID = iconIdMap.containsKey(id) ? iconIdMap.get(id) :
// Fallback/unknown icon
R.drawable.ic_action_achievements;
// Load and return bitmap
InputStream is = getResources().openRawResource(resID);
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}

Related

Getting image download links from owncloud server

there is not much information out there for the newbies to owncloud.. not even here in Stack Overflow, a little background on what im trying to achieve is:
-Already managed to sync my project to the owncloud library via gradlle.
- The provided examples are not of much help for me you will see why.
I use this library https://github.com/bumptech/glide to stream images URLs to some imageviews in my app, as i was on a web server I had no problem but now that I moved to owncloud, I can do the same if i provide my app with the "download link" from the stored images.
That said, what I need is to some how be able to select the owncloud stored images(access the shared folder and download) by name and or read the download link from where ever is stored..
here is the use the glide library on my project
String url = null;
if (plato.getFoto_movil().equals("churrasco.jpg"))
{
url = "http://192.168.0.20/owncloud/index.php/s/EqX7LxLpUeBCzF4/download";
}
if (plato.getFoto_movil().equals("pizza.jpg"))
{
url = "http://192.168.0.20/owncloud/index.php/s/VGQJh6ii36PLGsN/download";
}
if (plato.getFoto_movil().equals("torta_chocolate.jpg"))
{
url = "http://192.168.0.20/owncloud/index.php/s/K0TaHRMPuMrs0Fx/download";
}
Glide.with(mContext).load(url).into(imageView);
which is sad because I had to manually get those URLs from my browser and it does not work with the newly added images from any othere device, so I need to be able to get any new image and show it too.
I have the library installed 100% and implemented but
private void startDownload(String filePath, File targetDirectory) {
DownloadRemoteFileOperation downloadOperation = new DownloadRemoteFileOperation(filePath, targetDirectory.getAbsolutePath());
downloadOperation.addDatatransferProgressListener((OnDatatransferProgressListener) mContext);
downloadOperation.execute( mClient, (OnRemoteOperationListener) mContext, mHandler);
}
#Override
public void onTransferProgress(long l, long l1, long l2, String s) {
mHandler.post( new Runnable() {
#Override
public void run() {
// do your UI updates about progress here
}
});
}
#Override
public void onRemoteOperationFinish(RemoteOperation remoteOperation, RemoteOperationResult remoteOperationResult) {
if (remoteOperation instanceof DownloadRemoteFileOperation) {
if (remoteOperationResult.isSuccess()) {
}
}
}
got those and
got also this two
mClient = OwnCloudClientFactory.createOwnCloudClient(serverUri,mContext,true);
mClient.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(username,password));
then I call the method
startDownload(plato.getFoto_movil(),downloadfolder);
the app crashes here when I call the startdowonload method
with the error java.lang.ClassCastException: com.eidotab.eidotab.MainActivity cannot be cast to com.owncloud.android.lib.common.network.OnDatatransferProgressListener
another relevant information is that im implementing the ownclod methods to
the viewholder of a recyclerview
public class VH extends RecyclerView.ViewHolder implements OnRemoteOperationListener, OnDatatransferProgressListener
but I could change to the main activity if needed, probably make it work from there but even if it does have success, donĀ“t know where or how to get the download links, like I said, I need the download link from every image stored in the server..
is this possible?
thanks in advance
I changed the implementation of the owncloud library to the main activity as suggested (and was the logical move) and everything went smooth.. worked at first try, I knew it was going to, the thing is that I didnt want to store the images on the device memory, I want to capture the download links which was my main concern but for now it is working since im streaming the images directly from the local sd to the imageview and that should give me some more time to investigate further in the matter whats the thing with the URLS...
Thanks cricket for your comments

Android TagManager not getting default values

I am trying to implement the fairly new Android TagManager from Google. I can't seem to be able to load the default values.
I have created my default json file:
assets/tagmanager/GTM-xxx.json
Which looks like this:
{ 'eulaTextVersion': '1' }
I have also added this code to actually pull the default file if nothing is found on the server:
TagManager mTagManager = TagManager.getInstance(this);
// The container is returned to containerFuture when available.
ContainerOpener.openContainer(
mTagManager, // TagManager instance.
CONTAINER_ID, // Tag Manager Container ID.
OpenType.PREFER_NON_DEFAULT, // Prefer not to get the default container, but stale is OK.
null, // Time to wait for saved container to load (ms). Default is 2000ms.
new ContainerOpener.Notifier() { // Called when container loads.
#Override
public void containerAvailable(Container container) {
// Handle assignment in callback to avoid blocking main thread.
mContainer = container;
}
}
);
int eulaTextVersion = (int) mContainer.getDouble("eulaTextVersion");
However, when I debug, my int eulaTextVersion is always zero, I can never get it to 1 like it should be from my default json. Could someone please help me out and show me where I am going wrong?
Thank you for the help.
Probably Google Tag Manager already downloaded first version for you. First version is probably empty. Just make next version with "Values" that contain "eulaTextVersion" and "Publish" it.
If you do not know how to set those values on your Google Tag Manger account look at: http://youtu.be/Xe8W5w68BRg?t=8m26s
You shouldn't be using quotation for numerical values. Try the following instead
{ 'eulaTextVersion': 1 }

How to I get the cached Bitmap I downloaded using Volley ImageLoader?

Problem: I've a X amount of ImageViews that I'm adding dynamically like this:
for (int i=2; i < result.size(); i++) {
// instantiate image view
ImageView mImageView = new ImageView(this);
mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
mImageView.setBackgroundResource(R.drawable.selectable_background_theme);
mImageView.setOnClickListener(this);
// download image and display it
mImageLoader.get(result.get(i), ImageLoader.getImageListener(mImageView, R.drawable.ic_logo, R.drawable.ic_action_refresh));
// add images to container view
mLlDescContent.addView(mImageView);
}
What want to be able to click on the image and display it in another activity in full screen. I have read about a couple of ways such as passing the Uri or passing the actual Bitmap as a byte array.
Question: How do I get the Uri or the actual Bitmap I downloaded with Volley ImageLoader. The LruCache I'm using an BitmapLruCache I found here: Android Volley ImageLoader - BitmapLruCache parameter? . Can someone help me with this or any idea to accomplish my goal.
I tried this after the above code and nothing:
Bitmap mBitmap = VolleyInstance.getBitmapLruCache().getBitmap(result.get(2));
mIvAuthorImg.setImageBitmap(mBitmap);
Edit: If i re-request the image with:
mImageLoader.get(result.get(i), ImageLoader.getImageListener(mImageView, R.drawable.ic_logo, R.drawable.ic_action_refresh));
the image is loaded from the cache, BUT if I try to access the image straight from the cache with:
Bitmap mBitmap = VolleyInstance.getBitmapLruCache().getBitmap(result.get(2));
mIvAuthorImg.setImageBitmap(mBitmap);
the image don't load. I want to be able to manipulate the image, such as size an stuff before a pass it to the next activity.
Volley depends on your implementation of a cache for successful efficient caching.
The constructor for the ImageLoader takes in an ImageCache which is a simple interface in Volley to save and load bitmaps.
public ImageLoader(RequestQueue queue, ImageCache imageCache)
A quote from the Javadoc of ImageCache interface:
Simple cache adapter interface. If provided to the ImageLoader, it will be used as an L1 cache before dispatch to Volley. Implementations must not block. Implementation with an LruCache is recommended.
Darwind is right. If you request an image and it is present in the cache it will be loaded from the cache and not from the web. This should be the case for you since you're loading and presenting an image, which if clicked should be displayed from the cache in your new activity.
You say it's not working, perhaps your implementation isn't optimized for your use case. What kind of cache are you using? Do you have one centralized RequestQueue and ImageLoader as is recommended by the Volley team?
Take a look at this question, which isn't exactly the same as yours, yet could be helpful to you. It has a simple LRU cache implementation.
Hope that helps!
Edit:
The point of Volley is not to worry about implementation details. You want an image? it will load it for you the best and fastest way possible (from memory and if it's not there via network). That's exactly the way you should look at it. Retrieving the cache and then looking in it is not the right approach IMO.
Now, if you want to manipulate the bitmap, you have a few options, the best IMO being to implement your own Image Listener pass it to the get() method instead of the default one.
Something like this:
public class MyImageListener implements ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {
// handle errors
}
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
//
// manipulations
//
// assuming mView is a reference to your ImageView
mView.setImageBitmap(bitmap);
} else {
// display placeholder or whatever you want
}
}
}
from the Javadoc:
The call flow is this:
Upon being attached to a request, onResponse(response, true) will be invoked to reflect any cached data that was already available. If
the data was available, response.getBitmap() will be non-null.
After a network response returns, only one of the following cases will happen:
onResponse(response, false) will be called if the image was loaded.
or
onErrorResponse will be called if there was an error loading the image.
There's a bug (I'm 70% sure it's a bug) in volley currently where if a cache-expiry isn't specified (say, if you're getting an image from an S3 bucket where you have a never-expires setting), you'll always redownload from the network.
You could get around this by checking out HttpHeaderParser, and changing the relevant bits (this isn't totally crazy, as you have to include the volley source code anyway) to:
// Cache-Control takes precedence over an Expires header, even if both exist and Expires
// is more restrictive.
if (hasCacheControl) {
softExpire = now + maxAge * 1000;
} else if (serverDate > 0 && serverExpires >= serverDate) {
// Default semantic for Expire header in HTTP specification is softExpire.
softExpire = now + (serverExpires - serverDate);
} else if (serverExpires == 0) {
softExpire = Long.MAX_VALUE;
}
Then you could just pass the Uri to the activity opening the thing as a parameter. This solution has the enviable property that if something goes wrong and something happens to the image between launching the activity and displaying the bitmap, you'll still redownload it and things will work appropriately. That'll probably never/seldom happen, but it's nice to know correctness is preserved.

Android Volley - Some disc cached images dont get displayed?

I've run into this weird error, where some images get cached as usual and some don't, any idea why?
Both images do get displayed and memory cached just fine, but when offline some display error image.
For example, this works fine:
http://cs4381.vk.me/u73742951/a_58a41ac2.jpg
However, this does not: http://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Android_robot.svg/220px-Android_robot.svg.png
Both work fine displaying and memcaching but the second doesn't get displayed from disk cache, although I think I see it being saved, as app says it has 12kB cache in the system settings
Edit
I checked out a clean copy of Volley and it does the same thing. Its definatelly a bug...
From what Ive found out its that images do get cached, but Bitmap cachedBitmap = mCache.getBitmap(cacheKey); always returns null, so the cache says it doesnt have the bitmaps and then proceedes to download it again, and fail when offline, weird
The reason you're not getting any hits is because the default behavior in Volley for disk caching is dependent on the HTTP headers of the element you're requesting (in your case, an image).
Check the volley logs and see if you get the "cache-hit-expired" message - that means that the image was cached but it's TTL is expired as far as the default disk cache is concerned.
If you want the default settings to work, the images must have a Cache-Control header like max-age=??? where the question marks indicate enough seconds from the time it was downloaded.
If you want to change the default behavior, I'm not sure, but I think you have to edit the code a bit.
Look at the CacheDispatcher class in the Volley source.
Hope that helps.
A quick and dirty way:
private static class NoExpireDiskBasedCache extends DiskBasedCache
{
public NoExpireDiskBasedCache(File rootDirectory, int maxCacheSizeInBytes)
{
super(rootDirectory, maxCacheSizeInBytes);
}
public NoExpireDiskBasedCache(File rootDirectory)
{
super(rootDirectory);
}
#Override
public synchronized void put(String key, Entry entry)
{
if (entry != null)
{
entry.etag = null;
entry.softTtl = Long.MAX_VALUE;
entry.ttl = Long.MAX_VALUE;
}
super.put(key, entry);
}
}

Avoid Android Memory Leak when not using Static Image

I know this has probably been addressed, however, I am having memory leak issues in my Android app. I have it cycle through a differant picture in the users library every time they push a button. This works fine for the first couple then throws an out of memory exception. I have looked around and though I understood that the pictures are being stored on the heap(?) even after it is not being pointed at. Is there a way I can force this to clean up so I am not getting the error? I have tried the following....
private void setImage() throws RemoteException{
view.setBackgroundDrawable(null);
currentBackground = Drawable.createFromPath(backgroundImageService.getCurrentImageLocation());
view.setBackgroundDrawable(currentBackground);
}
UPDATE:: Update This worked!!!
private void setImage() throws RemoteException{
if(currentBackground != null){
currentBackground.recycle();
}
currentBackground = BitmapFactory.decodeFile(backgroundImageService.getCurrentImageLocation());
view.setBackgroundDrawable(new BitmapDrawable(currentBackground));
}
Thanks
you can use Bitmap.recycle() if you can change your Drawable with Bitmap.

Categories

Resources