First, get screen size of the device:
public void getScreenSize() {
Display display = getWindowManager().getDefaultDisplay();
Point sizepoint = new Point();
display.getSize(sizepoint);
width = sizepoint.x;
height = sizepoint.y;
}
Then use these instructions to list the preview sizes supported by the device::
Parameters p = Camera.getParameters();
List<Size> size = p.getSupportedPreviewSizes();
Size size_to_use = getOptimalSize(size, width, height);
and finally set the preview sizes to the camera preview:
p.setPreviewSize(size_to_use.width, size_to_use.height);
myCamera.setParameters(p);
Below, the getOptimalSize method (got from the net).
private Size getOptimalSize(List<Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.2;
double targetRatio = (double) w / h;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
for (Size size : sizes)
{
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null)
{
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff)
{
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}