Is there anyway to synchronously retrieve bitmap from Glide on main thread? - android

I would like to use the awesome Glide cache but not finding a way to synchronously check if an image is available and pull the bitmap if it is. I only see async methods. Am I missing something? I want the bitmap then a byte array, and have no plans to put it in an imageview. If its in the cache I want to sync obtains its byte array. Can this be done? I saw this: https://github.com/bumptech/glide/issues/495
But that just deals with setup issue for an imageview not really what I need.

Related

get bitmap after picasso sets image to an ImageView

I'm trying to save downloaded images in shared preferences in base64 format so that the app can work offline. I've used picasso
Picasso.with(mContext).load(urls.get(position)).into(imageView);
I request the server with a specific category of images and the server responds with a json containing the urls of those images. I found picasso to do this job for me as i need to write my async task for this.The images are loaded into ImageViews of a gridview image adapter.
What I want to do is that as soon as the image is loaded through the url using this function I want that bitmap image so that I can save it in shared preferences or serialize it something so that it can be used offline.
or suggest me a better solution.
Solved it!
Picasso.with(mContext)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView);
Thankyou so much guys <3

Spawning numerous AsyncTask instances - implications?

I am designing an Android application targeting >= API 17. I have created a class, DownloadImageTask which extends AsyncTask, and receives a string (URL) and an ImageView as arguments. In it, I am opening an HTTP connection, downloading an image from a URL, and using BitmapFactory to create a Bitmap object from the data, then setting the bitmap to the ImageView. The end result is a populated list of data which is available to the user to scroll through, with images populating as they can.
This appears to be a good design on the surface - but I am concerned that I am putting my app at risk for an OOM condition, or other violation of the user experience rules. I'd like to know if the way I've designed this is correct, or if not, how I should approach this.
Thank you in advance for your help.
Two considerations to your own approach:
You shouldn't pass the ImageView to the async task because in that way you are coupling your view and your service layer. So send to the async task the URL, and onPostExecute method call to Activity which implement an updateView (or the like) method.
About your OOM, you are right. The problem might arise if you use the original bitmaps which could have larger resolution than required. Therefore you should scale down the images you keep in memory.
The last issue might not be difficult if you use a few images otherwise could be problematic. So if you will be working with a lot of images and you are not forced to implement your own version, you should have a look to the existing libraries. Some are already mentioned:
Glide
Picasso

Recycle Bitmap in loop

Here look at the below code,
for (String path : all_path) {
bmp = BitmapFactory.decodeFile(path);
bitmapList.add(bmp);
}
and this code is driving me crazy. As in each iteration BitmapFactory.decodeFile(path) is called and driving memory to its pick as a result OutOfMemory exception occurs. I tried to use recycle() old bitmap in the loop before decoding new bitmap but it means no sense. I searched for the answer about using bitmaps in loop but failed to find one. What should I do? anyone help please.
You are adding all bitmaps to a List. However you are using the same reference variable bmp for all bitmaps so in each iteration they get replaced.
But in the List all the bitmaps are being added. If there are many bitmaps then it will eventually result in OutOfMemoryError
Better do not add all bitmaps in a List.
Try to recycle the bitmaps you are not using.
If you use all the bitmaps than add them to a cache and use them later.
Next when you want to replace one you will replace just one not all of them.
The change from below requires a bit more work but it works as I used it with a lot of bitmaps.
Please take a look here:
I implemented once something like this:
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Bitmaps can be huge. Why do you need to read them all to the memory? Normaly, you would read each bitmap on demand. Optionally, you can read thumbnails, which are much less memory demanding (BitmapFactory enables you to downsize the bitmap when reading - use BitmapFactory.Options, member inSampleSize).
Read this: http://developer.android.com/training/displaying-bitmaps/index.html

Prevent Picasso to fetch already loaded images and unable to use OkHttp2.0 for disk caching

Both of above is related. Following is my case
I am using Volley Library
Picasso(2.3.2) to load images
Images are fairly large,so I resize them to dimension 300x300
But during scroll of ListView/GridView, the images are reloaded again. Though the reload time is fairly small, I do not want the reloading of such nature.
So browsing, net I came across following
Use OkHttpClient caching mechanism
How to implement my own disk cache with picasso library - Android?
So I tried using OkHttp 2.0.0 into Volley Library
https://gist.github.com/JakeWharton/5616899
I think , from OkHttp 2.0.0 there is something to be changed on above gist.
So I followed this instead
How to implement Android Volley with OkHttp 2.0?
But the Volley library won't function now using method 2.
Finally, I am trying to use caching as mentioned on this
https://gist.github.com/ceram1/8254f7a68d81172c1669
So, my question is fairly simple, how not to reload the images that has been already downloaded. And if ,I have to use OkHttp 2.0.0 for disk caching, what are the ways, I should follow.
Stay with flow of how cache should work. Don't know Picasso cause I use Aquery, but they should use pretty much same flow whenever a bitmap(bm) needs to 'load' in some image view.
Flow:
If bm is thumb do chekthm
If bm large do cheklarge
On thmb:
If in memcache return bm for load to view
If in filecache return bm from there
In temp bm still null do network fetch to return bm
, with loads to respective caches and with 'ejects' 4 onFullCache.
For large bm , use same flow like thmb only config cache holds not as many entries.
Nowhere in there do you have to prevent a fetch . why because the process Only arrives at choice of network fetch when the bm Not avail from local options. At that point u either use default drawible from local res ( not correct bm for your logic) or you must do network fetch.

Downloading images using AsyncTask in android

I am stuck up at one point. I need suggestions for the same.
I am creating an application which involves json parsing. After the parsing I am getting data which involves event name, event description, event place, event image url and so on. The data is huge. You can imagine facebook kinda stuff.
Now the problem is the data is getting parsed but because the event image is in the form of url, I need to convert it into Bitmap.
I have kept all the process of json parsing and bitmap conversion into one AsyncTask(doInBackground()).
This is taking a lot amount of time. I want something like facebook that the data gets loaded and is shown to the end user but the images load slowly and steadily. (I mean when we scroll facebook, then images don't come up immediately).
I want a similar functionality. I need suggestions.
You can still use an AsyncTask, only you'll need to use two seperate tasks.
One for the loading and parsing of the JSON, and one for the loading of the image.
After parsing the JSON, you'll need to start an AsyncTask for every image you're trying to load, making them all load on their own thread. It will show once the item is done loading.
Here is solution for you https://github.com/nostra13/Android-Universal-Image-Loader
You just pass url and imageview resource to loader, and it handles everithing for you. Also support caching. This library is simple and widly used
This concept is called Lazy Loading (AFAIK). You can use already development ImageLoader for this. It will download Image in Background and once it download it will set as background of ImageView. See Custom Downloader

Categories

Resources