Navigation Drawer - Call up camera via Menu

Asked

Viewed 124 times

2

I’m having a problem, take the Layout of Android Studio Navigation Drawer to do my project, but when I want to call the camera class the error.

I created another class for the camera, where I have verification of the GPS and the location where the image will be stored.

public class Camera extends Principal 
 {
private AlertDialog alerta;
public static final int IMAGEM_INTERNA = 1313;
public void Tirafoto (View view) 
  {
    LocationManager locationManager = (LocationManager)
    getSystemService(Context.LOCATION_SERVICE);
    final boolean GPSEnabled = locationManager.isProviderEnabled
(LocationManager.GPS_PROVIDER);
    if (GPSEnabled) {
    Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, IMAGEM_INTERNA);
    } else {
        AlertDialog.Builder alertas = new AlertDialog.Builder(this);
        alertas.setTitle("Ligar GPS");
        alertas.setMessage("Para usar essa função você deve ativar seu GPS");
        alertas.setPositiveButton("Ativar GPS", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        alertas.setNegativeButton("Não Ligar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
 Toast.makeText(Camera.this, "Camera disponivel somente com o 
 uso do GPS=" + arg1, Toast.LENGTH_SHORT).show();
            }
        });
        alerta = alertas.create();
        alerta.show();
    }
}

}

And in Main Activity I have

if (id == R.id.nav_camera) {
        Camera camera =  new Camera();

I do not know how to continue, because I tested several ways but I could not make it work.

  • I know it may just be a "draft" or I don’t know what, but I point out to you to check some spelling errors in a few words of your code.

  • Yes, and just a draft before putting in my correct design, but I don’t understand where there are spelling errors.

  • Camera available only with GPS Usu

1 answer

0

I simulated your project here and the error that happens is: java.lang.Illegalstateexception: System services not available to Activities before onCreate()

I don’t understand the reason for the Main Activity Camera Extender class. Why you don’t call the code directly at the Navigationdrawer item click:

if (id == R.id.nav_camera) {
        // Handle the camera action
        LocationManager locationManager = (LocationManager)
                getSystemService(Context.LOCATION_SERVICE);
        final boolean GPSEnabled = locationManager.isProviderEnabled
                (LocationManager.GPS_PROVIDER);
        if (GPSEnabled) {
            Intent camera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(camera, IMAGEM_INTERNA);
        } else {
            AlertDialog.Builder alertas = new AlertDialog.Builder(this);
            alertas.setTitle("Ligar GPS");
            alertas.setMessage("Para usar essa função você deve ativar seu GPS");
            alertas.setPositiveButton("Ativar GPS", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });
            alertas.setNegativeButton("Não Ligar", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(MainActivity.this, "Camera disponivel somente com o uso do GPS=" + arg1, Toast.LENGTH_SHORT).show();
                }
            });
            alerta = alertas.create();
            alerta.show();
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.