I implemented a RecyclerView in one of my Activities.
I use a customized RecyclerView adapter to load data from a SQLite Database (using Room and LiveData-structure) and display it in the view.
All of the rows have a large ImageView where I want to show a Drawable (just a .png). To do that I first tried to use the approach written on the official Android Developers site but it did not work well for me.
Therefore I am trying to use Glide as a library recommended by Google.
I have 16 drawables in my project. They all have a scaling between 800x800 and 1920x1080 pixels.
In my adapter I load the data from my database and based on the drawable id, the image as well (so there is no image data stored in the database; I did this previously).
Unfortunately my App cannot handle that amount of image cache which leads to an OutOfMemoryError exception. That's why I used
android:largeHeap = "true"
in my Android Manifest.
I know this is not a good solution and I also know that there has to be a way to use less memory for such a small amount of pictures.
In Glide I use
GlideApp
.load(R.drawable.my_drawable)
.fitcenter()
.into(myImageView);
But unfortunately the image does not get shrinked or smaller. I thought that Glide can scale the image based on the size of the ImageView, screen size and so on.
What am I doing wrong? Is there a better way to use less memory without fixed scaling so that my pictures not become ugly?
EDIT:
I first stored my images in the basic "drawable" folder in my project structure (copy and paste).
My second approach was the gimp-android-xdpi addon which exports images or icons for any android density (mdpi, hdpi, xhdpi etc.). But this did not help either.
call override(horizontalSize, verticalSize).
This will resize the image before displaying it in the ImageView.
Related
I have an expandableListView in my activity and passing data from JSON that I get from server. Everything is working fine Except the image part.
I'm getting image path as string within JSON for different sizes (48x48, 72x72, 96x96 etc.) but unable to figure out how to assign image according to the screen density. I think I can Add these images into the corresponding drawable folder and android would do the rest. But on searching I did not find any example on how to add images in drawable folder dynamically.
If there is any way to add images dynamically into drawable folder please let me know OR suggest any other appropriate solution.
You can't add images dynamically into the drawable folders.
But you can get the device's screen density programmatically. You can use that value in your api and return appropriate image(s).
You must understand whats drawables in android by this link
and you must know that you can't put any images into them dynamically, icons and static images must be into drawables folders, for other images which you are using them into application you can get image and thumbnail
it's not possible to save in drawable at runtime.
you can't use assets folder too.
if I were you it I didn't bother my self to determine screen density. use Glide and Picasso in order to load image asynchronously.
Update :
the thing that i should mention is that, if the ImaveView is a fixed size view by dp it'll appear "the same size" on all devices as per Android's specs.
glide and Picasso read the view size by pixel and then scale the image to that size.
I prefer Glide because it has a smaller memory footprint by making some different assumptions like caching the resized images instead of the full-sized images.
Edit:
#greenapps you are completely right. it cannot be saved in assets too. try to create a directory and save drawable into that
I would like to create fragment with image as background. What I do is saving image in .png format, putting it to drawable folder and than using it as background in layout. First thing I noticed is that the view stutters, so I created different images for different densities. But I'm still afraid that in older devices with worser CPU view would statter.
Question
Do I have to use libraries like Picasso for adding images as background ? What is the best approach ?
I think that base approach of using libraries like Picasso, Glide, UniversalImageLoader is for downloading images from network, caching, decoding, smooth loading in runtime.
If you want just set background for imageView from your local resources the only thing you should consider is to do this efficiently. This is nice described by google here and should be enough for your case.
In my app I'm generating a bunch of Bitmaps at runtime to show in a GridView. The generated Bitmaps consist only of rectangular shapes and about five different colors.
If I make them big, they get scaled down nicely, but I get OutOfMemoryExceptions. But when I make them small, they're not scaled up to fit the column width. I think ImageView can't help me, because it doesn't know the final column with. Setting stretchMode to columnWidth in the GridView didn't help.
Setting adjustViewBounds to true on the ImageView helped with large Bitmaps, but it doesn't help for upscaling.
Is it somehow possible to scale the ImageView with the underlying Bitmap to the maximum column width of the GridView? This would be my preferred solution.
If not, can I determine the columnWidth of the GridView in advance to just generate the bitmap accordingly? (I don't like this solution that much, because I suspect that on devices with large screens I might run into OutOfMemoryExceptions again.)
You have two choices.
METHOD 1:
Optimize your images by using any online image compression sites . For example https://tinypng.com .TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size!
METHOD 2:
Load your images using third party libraries like Universal Image Loader, Glide .. these libraries aims to provide a powerful, flexible and highly customizable instrument for image loading, caching and displaying. It provides a lot of configuration options and good control over the image loading and caching process.
Since you generate the bitmaps in your app, you can use libraries like Picasso to display them. Picasso will handle the memory on your behalf and you need not worry about OutOfMemory Exceptions.
I want to use layout in this way, where image will be loaded from a HTTP server request. I need to know how can I define the image sizes from various android device , as in the server there will not be any drawable asset folder.
Till now I have fixed the height of the Relative Layout inside which the ImageView is placed,which has height and width : fill_parent, and scale_type : fitXY, for which if the image is small its getting stretched.
And this is not a good approach. Can you please suggest me a good way, as how can I define the image sizes.
Good question. There are two solutions that come to my mind:
Similar to how Andorid maintains resources (high display, extra high display, low display, etc.), Request your server to resize photos before sending them
Downloading full resolution photos and resizing them in your app.
Option #1 is more optimized, but #2 is easier to implement. It totally depends on how much time you're willing to spend on this.
It's also highly recommended that you use an image loading library for downloading images. Glide and Picasso are probably two of the best libraries out there. Pass an image URL and they'll handle everything: Downloading the photo, caching it in memory + storage, resizing, etc.
Oh and I don't think fitXY is the best option for your purpose. Try using centerCrop which crops your images to fit them properly.
i am new to android and i want to set an image as background to different fragments in an activity.But the images are to large that they make my application to increase in memory and i don't want this. The activity have 6 different fragments and each have different background, here i set the background images from drawable.
i referred this.but i didn't get correct solution.
How could i make them so that the memory size of images will be less?
Android - Reduce the memory usage of Bitmap Drawables
On this concrete case you have 2 options:
1 - Set the image as background on the root view of your fragment Layout. Doing that you'll avoid out of emmory errors, but the image will be scalled to fullfill the whole screen, so it could be diformed.
2 - Use Picasso library http://square.github.io/picasso/ to load the file images. It can be helpful to manage memory issues.
Also, the best thing you could do before starting is to reduce the size of the images using some software such as https://tinypng.com/.
Hope it helps