Bitmap getWidth returns wrong value
This is probably because of differing densities. Your resource is probably stored in a medium density folder, but your device is hdpi. Medium density is 160dpi, high density is 240dpi. Thus your bitmap is scaled to 1.5x the size it was originally. See the document on multiple screens for more info.
If resources are not available in the correct density, the system loads the default resources and scales them up or down as needed to match the current screen's density.
If you intended this to be for high density put it in drawable-hdpi instead of drawable or drawable-mdpi.
Update:
If you want it to ignore density, put it in a drawable-nodpi folder. Also from the same doc:
The easiest way to avoid pre-scaling is to put the resource in a resource directory with the nodpi configuration qualifier. For example:
res/drawable-nodpi/icon.png
When the system uses the icon.png bitmap from this folder, it does not scale it based on the current device density.
You can also see here for more information on providing and grouping resources.
Why are the values returned by getWidth() and getHeight() double the actual image dimensions?
You using decodeResource
, so image is scaled based on your DPI. To scale it to size you need, place it in different folder. I suggest drawable_xhdpi is what you need, if scale factor is 2.
And keep in mind, it will be scaled on devices with different screen density.
To completely avoid scaling you may put your image to assets folder for example.
BitmapFactory.Options#outWidth returns different size than actual bitmap
The first 3 lines don't affect the 4th line since the 4th line don't use any of the variables above it. also the "bmpOptions" doesn't get used or filled at all. maybe you've renamed the variables while posting this question?
also, is it possible that the bitmap image file is located on folder that has a density qualifier (or the default one which is like mdpi) ?
only if you put the file into the drawable-nodpi, you will always get the same number of pixels for width&height.
getWidth and getHeight are returning a zero
The width and height are not defined until the view is actually rendered to the screen.
Use protected abstract void onLayout (boolean changed, int l, int t, int r, int b) (which you override in your activity) to know when the sizes are ready.
Android: Bitmap from Camera Intent retrieving wrong Height and Width Data
The Bitmap
that you receive in onActivityResult
is a thumbnail.
If you want to have access to the actual file, you should provide the URI where you want the image to be saved with the EXTRA_OUTPUT intent extra:
Intent mCameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputUri);
mCameraIntent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(mCameraIntent, mCameraRequest);
getMeasuredWidth returns totally wrong value
Passing WRAP_CONTENT
instead of an actual value to makeMeasureSpec()
won't do any good, I don't think those were meant to be used together.
Use actual constraint value as first parameter (e.g. size of the parent view or dimensions of the screen) with the MeasureSpec.AT_MOST
mode, which is basically Math.min()
-- with arguments being the value you put in the spec and the view's desired dimension.
MeasureSpec.EXACTLY
will make it use the spec value, MeasureSpec.UNSPECIFIED
will make it use the view's desired value. So if you don't have any constraints (like if you put your view into a ScrollView
) your option is MeasureSpec.UNSPECIFIED
-- and any value as first argument.
So, {gurus, correct me if I'm wrong} you get WRAP_CONTENT
behavior with any mode other than MeasureSpec.EXACTLY
That being said, try:
ViewGroup view = ...;
view.setPadding(10, 0, 10, 0);
// Either this
int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);
// Or this
int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();
PS Funny thing about 16777214, take a look at the number in binary: https://www.google.ru/search?q=16777214+in+binary It's -2 (which is the constant value of LayoutParams.WRAP_CONTENT
) but with 8 most significant bits truncated with a logical operation (hence no minus), there's something in call stack of measure()
that does this :) I wonder if this is a bug. Maybe it should be checking the argument for being negative and throwing an exception or something.
Related Topics
Is There on Install Event in Android
Efficiency of Searching Using Wherearraycontains
Passing Function as a Parameter in Java
Setting a Spinner Onclicklistener() in Android
How to Convert a String Date to Long Millseconds
== and .Equals() Not Working in Java
Why Do We Have to Call Super in Android Sometimes
How Many Ways to Convert Bitmap to String and Vice-Versa
Gradle Sync Failed: Unable to Find Method
Endless Scrolling Listview Not Working
Eclipse with Android Sdk, Error: Java Heap Space
What Does a Java Static Method Look Like in Ruby
Create Random Pixel Images in Swift
Pyspark: Exception: Java Gateway Process Exited Before Sending the Driver Its Port Number
The Specified Dsn Contains an Architecture Mismatch Between the Driver and Application. Java
How to Change the Edittext Text Without Triggering the Text Watcher