I'm capturing an image using camera (And it is saved in the pictures folder). I want to resize it before convert it into String and upload it into a server. I have a constraint that the maximum file size should be about 850 kilobytes.
I have a knowledge about Bitmap.compress method which only reduces the image quality using a percentage. But the difficulty in that is that the different devices might produce images with different sizes. So I'm unable to use static percentage to reduce the size.
I have the image url and it can be converted to a Bitmap. Can I reduce the size of an image for any value under 850 kilobytes?
Related
I am using 10 png images of size 20-30kb in imageView, but the allocated memory increases from 70mb to 270mb when this activity loads.
So why this too much of memory is allocated to these images.
This is the screenshot of memory allocation
This is one of my image
File size doesn't matter. No, matter your image is of 20kb but its resolution is quite big. When image is loaded into memory it takes memory equal to totalNoOfDotsInImageBitmap * 4bytes
and totalNoOfDotsInImageBitmap = width *height of image.
4 bytes - because of ARBG ( 1 byte per argument) for single dot of bitmap
so reducing width and height of image may solve your problem.
Depending on where you are putting the assets it may be trying to load a file that is too large.
For instance, if you have it on drawable or nodpi-drawable a device with a low density will try to load a potentially large image.
Also, bear in mind that the actual file size is not that important as it is probably small due to compression, but the image has to be converted to bitmap when it gets drawn, so if the actual size is too much that can also cause an OOM.
If you have access to the original I would recommend using a vector drawable (it's a simple shape so should be ok) and AS will generate the required PNG files for older versions.
I have 3 URLs on gridview and only 2 of the 3 images load into the ImageView.
I am using Picaso:
Picasso.with(c).load(imageUrl).placeholder(R.drawable.progress_animation).fit().centerCrop().into(ivPicture);
I have also tried to skip caching:
Picasso.with(c).load(imageUrl).placeholder(R.drawable.progress_animation).fit().centerCrop().memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).into(ivPicture);
Additional Info of image:
Width 2048 / Height 1371 pixels
File size = 224KB
File format is JPG
When I use Picasso's .resize(h w)to a lower the resolution eg: resize (100 100) for testing the image is downloaded but the aspect ration is not kept as some images will have a 9:6 ratio or vice versa 6:9 or a perfect square 5:5 and therefore I do not want to resize any images to a fixed ratio instead I use .fit().centerCrop() which works very well except for this 1 high-res image, Picasso just loads on the placeholder endlessly without any errors, I can understand if the pixels are too many but the file size is quite small in size I don't know why Picasso is struggling to download a +/- 200KB image.
Is there a way for Picasso to compress images over certain pixels perhaps?
Picasso can not process very big images because will receive out of memory .You must to do image transformation inside native code .Not in java
i have two jpg images.one image size is around 1 MB another is lower than 100KB.but both the two images has the same resolutiuon. now these images should be loaded into imageviews. the memory allocation while loading these images will be depends on filesize or resolution ?
It depends on the resolution, not the file size. The image will be decompressed into memory when loaded, so two images of the same resolution will use the same amount of memory.
You can verify the memory usage of a Bitmap loaded into an ImageView using getByteCount() or getAllocationByteCount() (KitKat and above).
e.g:
ImageView image = (ImageView)findViewById(R.id.image1);
int bytesUsed = ((BitmapDrawable)image.getDrawable()).getBitmap().getAllocationByteCount();
Whenever I try to show large size(1 MB) bitmap in imageview, system throw OutOfMemory exception.
If I place 7-8 MB image, gallery can easily show that image.
Just want to know How device default gallery is able to show large image in easy way?
Which mechanism is used by device gallery?
Generally speaking, they subsample the image. A 1MB PNG or JPEG file will be much higher resolution than the screen, and so it is wasteful to load the whole image in. Subsampling allows you to load the image in much closer to the actual size of the ImageView you will use, allowing it to fit in memory better. A simple approach to subsampling involves using inSampleSize on BitmapFactory.Options; Dave Morrissey's SubsamplingScaleImageView offers pinch-to-zoom and such while peforming subsampling (note: I have not tried this component, as I just ran across it a minute ago).
Actually i am developing one application, where my requirement is like i have to scale large size images to some predefined size as per user selection. eg,Suppose i have an image which is of 1Mb and user select it to convert it into an image of size 100kb then i have to resize that image to 100kb.
last one day i am trying to achevive this with Bitmap APIs but i am not able to achieve the exact size what user want. Sometimes its very large sometimes very small. So please if anyone knows how to resize image in android to exact size(which is changing as per user selection).
Please help me out in this.
If only the file size matters, you will want to use an image file format that does not use compression. You should be able to take an image with attributes such as width, height, bit-depth, and so on and calculate the expected file size for such an image.
So, using such a file format you start with your original image and you have
File size = width x height x bit depth + metadata/overhead.
I assume you want to maintain the original aspect ratio also. In that case, you can probably just figure out the % reduction in file size from the current file size, and multiply the width and height by the same %. Scale your image using a bitmap image manipulation API and then save it. It should be close to the file size you are looking for.
Specific to the Android Bitmap api you can use getByteCount() to determine how many bytes the image currently takes up. You can also use getConfig() to determine how many bytes per pixel.
So, your final goal file size converted to bytes divided by the number of bytes per pixel, gives you the number of pixels you are allowed. Number of pixels allowed divided by the number of pixels in your current image will give you the scaling factor. Use the scaling factor and scale the image keeping the current aspect ratio and you should have a bitmap with a number of pixels close to your goal. Then save in a file format that does not use compression.