I am making an application where I need to display PDF pages inside ViewPager. We create Bitmap from each PDF page and then set it to ImageView. Creating bitmap is expensive process so I decided to cache bitmap once it gets created. This also improved the performance in terms of memory and loading bitmap. Since this is not a http request so I couldn't use any image caching library like Picasso. Instead I have used google's this sample and added the conversion logic of PDF page to Bitmap here.But the problem is when I swipe pages wrong bitmaps gets attached to wrong ImageViews.
For example I am swiping to page no. 5 and before the bitmap loads if I move back to 3 then the bitmap which suppose to attached to 5th page gets wrongly attached to 3rd page.
I tried a lot but couldn't fix it. Any help would be greatly appreciated.
Related
suppose that I have just one ImageView inside a HorizontalScrollView. The problem I'm facing is I have to create a very wide Bitmap to place it inside my ImageView and, obviously, use the scroll from HSV to see all my image there. I'm getting a lot of OutOfMemoryException so, is there any technique to get this task done without getting a ton of OutOfMemoryExceptions?
You are getting OutOfMemoeryException because your bitmap is too large to load the entire bitmap into memory (a loading issue not a rendering issue).
Instead you need a custom image view that downsamples and/or only loads sections of the image at a time depending upon what part of the image should be currently visible.
This may not fit your use case but it is an example of this problem being solved by downsampling
I need to show 1000 images in viewpager from DB.The problem is if I fetch all images from DB and try to set those images to adapter,it shows Bitmap out of memory exception since heap size gets exceeded. So,I'm trying other way around which is to load images one by one when the user swipes to next page.I Googled a bit,but didn't find any appropriate solution.so any input on this is highly appreciated.
Here is all that you need : http://developer.android.com/training/displaying-bitmaps/index.html
Load Bitmaps efficiently, LRU cache, etc. etc.
To summarize, you should fetch one bitmap at a time, scale it down using inSampleSizeand use LRUCache. OR, you can use http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149 library
I'm using Picasso Square library in my android application. The app is a very simple one and shows a grid of pictures. When you touch one it opens it in full screen and when swiping to the right the next in line should be displayed.
My problem is that for every swipe, the image is loaded by picasso method:
Picasso.with(getApplicationContext()).load(Properties.IMAGE_URL + i).transform(transformation).centerCrop().fit().into(imageView);
I would like to avoid the load wait time and simply cache the next 2 images to be displayed. how would I go about doing this?
I know that picasso caches the images if they were loaded before. Is there a way to load the next image with picasso without attaching it to a specific ui element to be displayed?
There is a way to do it – use fetch(). You can get more information from this question.
I currently have a gridview that displays some thumbnail images. The problem I am running into is that when I scroll through the images I can see the images switch from old to new as they are being replaced. So, I was wondering how I would fix this. Is there a way to load images that aren't on the screen so when I scroll the user doesn't have to see the change in images?
The best way to avoid that effect is to set the image first to null in the getView() method of your adapter. You can then check an LRU cache if the image is already loaded. If yes, then set it right away, if not then load it asynchronous as a bitmap. Set it to the view and add it to the LRU cache.
Context:
I have a main activity holding 6 tabs, each tabs holds a gridview with 30+ images, and around 8 images a shown at any moment (each gridview is scollable).
With these many images I implemented a simple caching system, that is to only cache the latest 25 of them, if new image is needed to display and is not in the cache, I will decode that image from /data/date directory and overwrite the oldest entry in the cache. And before overwriting the oldest entry, I will call Bitmap.recycle() on that.
Problem:
At some point, after displying more than 25 images, old images will start to be recycled. if I were to now navigate back to one of the tab where their Image is no longer in the cache, the GetView method will take care to load the picture again. The Issue is other than the first Item in the Gridview will be calling GetView, rest of the image will never get call, and the Gridview will attempt to display a cached image where it has been recycled already.
Any Suggestion? either to force get view or a smart way to cache image in this scenario?
Much Appreciated
Thanks
I kind of solved this by creating my own ImageView extending the Android ImageView. Then Overrided the onDraw(Canvas canvas) method and added logic to reload cache if cache is gone.
looks Hacky... but worked as wanted. really wish i cna do better than that if there is someone who can teach me. Thanks a ton!!