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.
-
Not very sure what you are trying to do? Are you trying to crop parts of the picture until you get the desired file size that the user wants? Or something else, also please include the error you are facing. – source.rar Jul 16 '11 at 15:33
-
If JPEG is used, you may vary the file size by changing quality level. However, there is no direct relationship between quality level and file size, so you may need to call `compress` multiple times with different quality levels: http://stackoverflow.com/questions/4579647/how-to-save-a-jpeg-image-on-android-with-a-custom-quality-level – rwong Jul 16 '11 at 18:42
1 Answers
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.

- 2,252
- 3
- 19
- 33
-
Actually i am not understanding how to get the value of bit depth from Bitmap. Will you give me some details about this? I am knew in this area so i dont know much about image scaling. – ani11 Jul 18 '11 at 07:04
-
You can use getByteCount() to get the number of bytes it takes to store the bitmap. Then you can use getConfig() to see how many bytes per pixel the current bitmap image format is taking up. – Shane Wealti Jul 18 '11 at 12:27
-
I tried with your formula but its not working out for all images. For some images its working and for some images its scaling down images to very small sizes. – ani11 Jul 19 '11 at 12:44
-
Do you have an example of an image that is not working that you could post somewhere? I would like to see the results of getByteCount() for the image before the resizing as well as the information returned by getConfig() for the image before resizing. – Shane Wealti Jul 19 '11 at 13:10
-
getByteCount() function is not working in android 2.3.3, so i try to use the fileSize for that part. and reagrding config config = bitmap.getConfig(); and its name is ARGB_8888. will it be possible to add some of your code for my reference. – ani11 Jul 19 '11 at 13:50