My code was working, I got the latitude and longitude normally, but it stopped and I am no longer able to get the location. So it falls on the condition that it was not possible to get the location
private void foto(int actionCode) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean GPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (GPSEnabled) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
switch (actionCode) {
case ACTION_TAKE_PHOTO_B:
File f = null;
try {
f = setUpPhotoFile();
mCurrentPhotoPath = f.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
} else {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (location != null) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
else {
Toast.makeText(Menu.this, "Não foi possivel obter sua localização" +
"", Toast.LENGTH_SHORT).show();
return;
}
} catch (IOException e) {
e.printStackTrace();
f = null;
mCurrentPhotoPath = null;
}
break;
default:
break;
} // switch