Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android get screen size via C

i want to get screen size via c. i know that jni support calling java from c. but is there any person who knows another method? i mean get screen size from lowlevel modules without calling java.

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    Log.v("getWidth", Integer.toString(getWindowManager().getDefaultDisplay().getWidth()));
    Log.v("getHeight", Integer.toString(getWindowManager().getDefaultDisplay().getHeight()));
    }
}

i think /dev/graphics is related to my questions

like image 846
osmund sadler Avatar asked Aug 26 '12 15:08

osmund sadler


1 Answers

Yes, you can get the screen resolution from /dev/graphics/fb0, but (at least on my phone), read access is restricted to root and users in the 'graphics' group. At any rate, you can do something like this (error checking removed for clarity):

// ... other standard includes ...
#include <sys/ioctl.h>
#include <linux/fb.h>

//...

struct fb_var_screeninfo fb_var;
int fd = open("/dev/graphics/fb0", O_RDONLY);
ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
close(fd);
// screen size will be in fb_var.xres and fb_var.yres

I'm not sure if these are considered "public" NDK interfaces, since I don't know if Google considers "Android runs on Linux and uses the Linux Framebuffer interface" to be a part of the public API, but it does appear to work.

If you do want to make sure it's portable, you should probably have a JNI fallback that calls into the Java WindowManager API.

like image 181
kelnos Avatar answered Sep 29 '22 04:09

kelnos