ImageView not able to show high resolution images - android

I am accessing images from android resources folder.
i am using asynctask to load images.
On first time i used images with resolution of 1024*768 the app opens and one by one image is shown into imageview.
on second time,
For experiment i used images with resoltion 1920*1080 the app is giving runtime error and force closed.
The same happens with image resoltion of 1280*720.
but it perfactly runs with 1024*768 resolution
why this happens????
i m not posting code cause it doesn't seem to have programatical issue.

Simply you got Out Of Memory exception because your image size is large, consider this calculation:
In order to show each pixel in ARGB_8888 we use 4 bytes so:
your first image: 1024 * 768 * 4 = 2MB
your second image: 1920 * 1080* 4 = 6MB
your third image: 1280 * 720 * 4 = 3MB
I do not know how many images you are loading to your RAM but your RAM is limited and it is at least 16MB so with other 4-5 images you will have no RAM to other objects. look at
Loading Large Bitmaps Efficiently to solve your issue.

Related

How to handle many images in an Android app

I am writing an app that has a lot of images (about 200) which are different items that you can select and order. So far I have been using Glide to load the images from file. To support different resolutions, I made a image of the item with my smartphone and converted them into the 5 different Android resolutions in the folders drawable-mdpi, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi and drawable-xxxhdpi.
The problem is that the original image has as size of 4,2 MB and all 5 images together just for one item for the different resolutions have a size of 18 MB (0,7 MB, 1,5 MB, 2,5 MB, 5,6 MB and 7,8 MB). If I now do this for 200 images, I will have to store 3,6 GB of images within the app. Thus, the size of the app would be extremely big.
So I would like to know what is a good way of dealing with so many images in an app? Is there a way to include multiple images for different resolutions while still having an app whose size is not enormous?
Follow the following steps:-
First of all, compress all of your 200 images. I will suggest you to use this website or any other.
After that add all the images to your drawables.
Convert all of your images to 'webp' using Android Studio.
1st Screenshot
Set the quality percentage according to your desire
2nd Screenshot
See the difference of image sizes
3rd Screenshot
In this way, quality will not be much compromised & large size problem will be solved.
-> Drawable Folder Position
loadGlide(R.drawable.testing, imageView)
Call this function from everywhere
Kotlin
private fun loadGlide(resId: Int, imageView: ImageView) {
Glide.with(context).load(resId).with(imageView)
}
Java
public static void loadGlide(int resId, ImageView imageView) {
Glide.with(getContext()).load(resId).with(imageView);
}
I took it with my smartphone camera and generally the images there have a resolution of 4032*3024
Have just one copy of the image, in res/drawable-anydpi/ to use it for any density.
The density-specific drawable system is designed for much smaller images than the full screen (icons, logos, etc.).

Android Image read , modify and save

I am trying to do the following steps -
Read an scaled image from assets and display in an imageview. - Successful
Modify the image - Successful
Save it to the SD card.- Not successful.
The resolution of the image is 4608 x 3456 with 300 ppi.
Since i was getting an out of memory exception on reading the image, i have scaled down the image to 800 * 800 while reading it using BitmapFactory.Options .
Now when i modify and save the image to SD card , the resolution of the image is 800 * 800 .
I want to get the exact same resolution as the resolution of the image in assets.
How to achieve this ?
Any help will be highly appreciated.
This SO helped me to create Image of required resolution i tried creating full hd image(1920×1080) i'm not sure whether you can create 4608 x 3456 resolution image.

Custom ImageView getting resized automatically on Nexus 7

I have a custom ImageView that covers the whole screen. The image is sourced from one of the drawable folders, drawable-hdpi, drawable-xhdpi, drawable-sw600dp etc. and there are separate images for each density bucket.
So far, the application works fine on xhdpi and hdpi devices, the problem occurs when I test it on a Nexus 7. The image stored in the sw600dp folder has a size of 1600x1600 and should be enough to directly draw on the screen (without scaling), however when I run the app, the screen turns out to be completely blank and I get the following warning:
03-04 16:25:46.338: W/OpenGLRenderer(25457): Bitmap too large to be
uploaded into a texture (2130x2130, max=2048x2048)
For some reason the 1600x1600 bitmap is scaled to 2130x2130 although I have not manually scaled it anywhere (no matrix postscale or scaleX/scaleY applied)
Any thoughts on why this might be happening? Please provide a solution along with the explanation of why this might be happening.
Since Nexus 7 is tvdpi, it scales every image to 1.33 of the original value. So 1600 becomes (1600*1.33)= ~2130. It wont display it from the sw600dp folder.
More Info:-http://developer.android.com/guide/practices/screens_support.html
You can use the same image which you are using for every other 7 inch tablet ( i.e. of resolution 1024*600 ).
I want to share some info about error you are getting..
According to Romain Guy (Android framework engineer):
When using hardware acceleration, there is a limit to the size of a
texture. When you render a bitmap, it has to be uploaded to an OpenGL
texture first. Your bitmap happens to be bigger than the maximum
texture size on Xoom (2048x2048.) You need to use a smaller bitmap or
split it into several bitmaps.
Other SO Links may be helpful to you : click here1, click here2
Git: click here
Update: This answer only tells why error is happening (becauseof texture size limit) sorry it's not a full answer for why 1600x1600 bitmap is scaled to 2130x2130!!

Android bitmap allocating 16 BYTES per pixel

I've got a program which is loading resources from the resource file (by setting android:src="#drawable/image" in the layout file.
The image is 800 x 1280 pixels. When the program runs it allocates 16,384,000 bytes on the heap which is 16 BYTES per pixel.
I assumed that it should default to RGB8888 which only uses 4 bytes per pixel. Anyone know how I can change this to use less memory?
Your images could be stored in an inappropiate folder. If you have high resolution images in a low resolution folder, let's say, the images are 800 x 1280 and you're storing it in drawable or drawable-hdpi, Android will scale them up to match xhdpi (although they are already in xhdpi, so to say). The scaled image occupies more memory.
Solution, put them in drawable-xhdpi.
you can import them using BitmapFactory, Options, and inSampleSize to reduce the memory footprint.
Also, that seems like a very high res image to display on a mobile screen so you could also reduce the size?
Check the documentation about bytes per pixel: http://developer.android.com/reference/android/graphics/Bitmap.Config.html
You can try using Color.RGB_565, but the image may lose quality.

Image size for android app

In my app I want use images for every product item. This image must be added to product when user create new item product. What the best approach about size image? (e.g. re-size image in 100x100 px or 200x200 px)
About size of Images and memory consumption of an application. This information can save you some painful time with android development.
take under account that most of the time this will affect your app performance
e.g. on most cases the used memory of the app will take:
x pixels * y pixels * 4 (bytes)
(you can change rgb encoding code to make the last parameter 2 or 1 that will effect image colors)
So 100 * 100 * 4 = 40000 bytes = 40 KB
but below and behold:
let's take a tablet setup of 1280 * 800 * 4 = 4096000 bytes, which is approximately 4 mega bytes. Taking under account that some devices max RAM for app is 16MB you might be scratching the limit at that setting.
And don't do what I did with:
2560 * 1600 * 4 = ~16MB
That will crash many android devices RAM limit with a little bit more than 16MB per image!! not so adaptable to most devices.
Personally I think that the image in Android development is not very convenient, but maybe they are working to improve it.
All you need to know about what resolution to use for what is given in the android official documentation. The best guide lines are posted here

Categories

Resources