I am little bit confusing because i am calculate the size of of an image.
I am using the following code in android:-
String fileUrl = getIntent().getExtras().getString("fileurl");
BitmapFactory.Options option = new BitmapFactory.Options();
option.inPurgeable = true;
Bitmap bitmap= BitmapFactory.decodeFile(fileUrl,option);
Log.e("Fill Image Size in Bytes","====>"+bitmap.getByteCount());
The above function bitmap.getByteCount() return different value compare to original size of image(compare with right click of image size in ubuntu).
If anyone have idea.please reply.
Thanks in advance...
You can decode the bitmap without options to see what's the difference.
Bitmap bitmap= BitmapFactory.decodeFile(fileUrl);
Log.e("Fill Image Size in Bytes","====>"+bitmap.getByteCount());
the default value of options may scale down the size.
It may be decided by Bitmap.config and bitmap size.
Related
I have read multiple posts like this about memory usage of background image.
my background image is 2048x1365 59KB JPEG; its uncompressed bitmap is 11MB
the background on the view for the particular device would be 480x605, so usage would be 1.1MB (480x605x4)
my app originally uses 12MB without background image
placing the image in drawable-nodpi/ and set it in the layout XML cause the memory usage to 23MB; so exactly base + BMP size
Using BitmapFactory to decode the image (moved to raw/) according to the advice results in 33MB of memory usage. (See codes below.)
Codes to set the background
View view = findViewById(R.id.main_content);
Rect rect = new Rect();
view.getLocalVisibleRect(rect);
BitmapFactory.Options options = new BitmapFactory.Options();
options.outHeight = rect.height();
options.outWidth = rect.width();
options.inScaled = false;
Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), backgroundId, options);
view.setBackgroundDrawable(new BitmapDrawable(getResources(), backgroundBitmap));
What goes wrong? What else can I do to shrink the memory usage?
The trick to getting BitmapFactory to give you a low-memory image is to fill in inSampleSize on the BitmapFactory.Options. This tells BitmapFactory to downsample the image as it loads, giving you a lower-resolution image, but one that is better tuned to whatever use you plan to put it to. You would need to calculate the desired inSampleSize that you want, based on the resolution of the ImageView (or whatever) that you are using the image for.
This sample app demonstrates loading some images out of assets/ with different inSampleSize values.
I have experienced this too but with much smaller images. I found out of that this was happening because I was using the same image size for all screen resolutions. I recommend you have different sizes of the same image and put them in the appropriate folders.
While I try to get image dimensions in pixels in an ImageView, I found that its width is 3 times more than the original jpg file width.
I put a jpg file which dimensions are 800 x 600, but the code below displays 2400 as its width.
Bitmap bitmap = ((BitmapDrawable)imgv.getDrawable()).getBitmap();
float fwidth = bitmap.getWidth();
Log.d("width0", Float.toString(fwidth));
I checked the jpg file size again but it was not changed (800 x 600),
I also searched for a solution but the code above displays the correct dimensions of the bitmap on other user's experience.
What have I done incorrectly?
Can anyone give me some advice?
Thanks for your help.
This is the solution found in a web site.
I needed to change the Option value when I decode the resource not to scale the original image. I had to use the three parameters for the decodeResource function, not two.
Of course, the third parameter was Options specifying the original bitmap not to be scaled. So now I can get 800 when I call bitmap's getWidth() function.
Resources res = getResources();
int id = R.drawable.map10;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
// options.inSampleSize = 3;
Bitmap bb = BitmapFactory.decodeResource(res, id, options);
float fwidth = bb.getWidth();
Make sure, that your ImageView is set to:
height : wrap_content
width : wrap_content
scaleType: none
I have a PNG image file with 2236x971px dimensions as a resource.
I want to scale it down by a factor of two (to its half). However, when i use this code:
BitmapFactory.Options bo = new BitmapFactory.Options();
bo.inSampleSize = 2;
Bitmap decodedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image, bo);
the decodedBitmap.getWidth() and decodedBitmap.getHeight() show width: 1677, height:728 → Only 25% reduction in size instead of expected 50%. Why is that so?
I am running the code on API 19.
The reason is that, your resource gets loaded according to your screen metrics. Put your image in the drawable-nodpi Folder or open an input stream to your resource and decode that input stream.
Is it possible to extimate a Bitmap's size in memory before actually decoding it? I was running into OutOfMemoryErrors and wouldn't want to try an allocation if there's not enough heap space left. If I set BitmapFactory.Options.justDecodeBounts to true I will get the width and height of the resulting Bitmap:
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, o);
Log.d(getClass().getName(),"Input image size: "+o.outWidth+"x"+o.outHeight);
Is there an easy way to get to the Bitmap's size without having to calculate it from the dimensions?
I was going to work around the issue by multiplying width*height*4 for ARGB_8888 to get the byte count of the resulting image. This is fairly precise but the resulting Bitmap's byte count is a bit higher than that. How would I go about and take this overhead of the Bitmap Object into account? I am not planning to re-use the bitmap so this should hopefully work with KitKat's getAllocationByteCount().
In my android application I am working on canvas bitmaps (actually it is SCanvas from Samsung SPen SDK, but it does not matter), as a rule 90-95% of area of such bitmaps is transparent, so I expected to have not too large bitmap size in KB. But seems like bitmap size (in KB) does not depend on whether it is a simple background or complex picture, so for example if I have two images (sorry, I am a new user and I can't post any images):
1) empty frame (1000x700 px, background is transparent, color border)
2) full frame (1000x700 px, background is transparent, color border, a lot of text inside)
the size of both bitmaps are about 1.3MB.
But if I convert these bitmaps in byte arrays, the size of the first array is about 11 times less than the size of the second array.
I have to store a lot of such images as BLOBs in DB and display them as ImageView bitmaps.
Question 1: if I need to display 20 ImageView objects with such 95% transparent images, is there any way to not use 20 * 1.3MB at the same time? To me it seems like it should be only 1 alpha layer + 20 "data" layers.
Question 2: is there any way to reduce size (in KB) of an image with transparency without losing too much quality? The only way I saw is to decode the image byte array with inSampleSize = 2 and then create a scaled bitmap to keep the original image dimensions, something like:
originalBitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
byte[] bitmapBytes = stream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options);
int bWidth = options.outWidth;
int bHeight = options.outHeight;
options.inSampleSize = 2;
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
options.inJustDecodeBounds = false;
Bitmap scaledBitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options);
Bitmap finalBitmap = Bitmap.createScaledBitmap(scaledBitmap, bWidth, bHeight, true);
But the finalBitmap quality is unacceptable, no matter what options I used (inPreferredConfig, inDither).
Any other thoughts about the optimal way to handle the images with about 90% of simple transparent background and 10% of one-color "data"?
No matter what format you use, it will always end up as raw uncompressed (decoded) data before displaying, if it weren't decoded, you couldn't see it on the display. If you're worried about memory, try not to have all the images decoded all the time, just decode the one you have to show, and always release the others (don't keep a reference to them).