My 64x64 png image is getting scaled down in a xxhdpi screen when it is placed in drawable-nodpi folder.
The code for decoding is:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon, opts);
I'm drawing it in a view with canvas.
Ah, i also read other post that manifest needs to have specified minSDK and screen supports but i still seem to be unable to achieve it.
Thanks in advance!
Related
I tried to get full size bitmap from drawable but not get.My image store into drawable folder and its sized 1082*1673 and when i get bitmap from drawable its sized 662*1024
I tried to all possible changes
My code shown below
[1] Bitmap icon = BitmapFactory.decodeResource(getResources(),R.drawable.t_0001);
[2] BitmapFactory.Options o = new BitmapFactory.Options();
o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.t_0001, o);
Put that image must be in drawable-xxhdpi folder
I guess if you put your images under the Assets folder (or subfolder inside assets), the auto resize will not happen.
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.
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.
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.
If I draw a bitmap on a view canvas using drawBitmap(), the images will be resampled so that 1 pixel in the image will be 1 dip on the screen. On a device I have with high pixel density, that means each image pixel is spread across 1.5 screen pixels, degrading the image. Handy in general, but in some cases I want to carefully select the images I want to draw, then draw them explicitly at their native size, so they won't degrade. How do I do this?
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
Bitmap mBitmap = BitmapFactory.decodeResource(mResource, R.drawable.resource, opts);
alternatively you could store your resources inside a res/drawable-nodpi folder