dora dora - 4 years ago 88
Android Question

Bitmap of x cm's width and y cm's height

I am taking a picture using camera in my app and will email the picture to a certain email address. now i want the emailed picture's physical dimensions to be 10cm's in width and 8cm's in height. and the code i have tried is as follows:

1) declare a imageview in xml like below :

android:src="@drawable/ic_launcher" />

2) and later in java file :

final BitmapFactory.Options opts = new BitmapFactory.Options ();
opts.inSampleSize = 2;
final ImageView thumbNail = (ImageView)findViewById(;
thumbNail.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

public void onGlobalLayout() {
// TODO Auto-generated method stub
thumbNail.setImageBitmap(Bitmap.createScaledBitmap (BitmapFactory.decodeFile(myPhoto.getPath(), opts), thumbNail.getWidth(), thumbNail.getHeight(), false));

As per the documentation the resulting bitmap should have width of 10cm's and height of 8cm's. but in xml file there is warning at these lines

Avoid using "mm" as units (it does not work accurately on all devices); use "dp" instead.

so i don't have a clue as to what i should do. so please give me a solution that will work on all devices.

Answer Source

i have used WarrenFaiths directions and came up with the below solution.

  DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
  totalDIP_X = metrics.xdpi;
  totalDIP_Y = metrics.ydpi;

totalDIP_X and totalDIP_Y represent the total number of DIPs per inch on the phone.

so if you want the bitmap to be 5cms*5cms on phone, which is equivalent to 1.968inch*1.968inch, we should use the below code.

  imageView1.setImageBitmap(Bitmap.createScaledBitmap (aBitmap, (int)(1.968*totalDIP_X), (int)(1.968*totalDIP_Y), false));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download