I tried to write an Android app. At the moment, I wrote a code that displays a preview of the camera and receives data from sensors on the device (gps reciver).
When I run the code in a separate application (for example, viewing the camera as one application and an application that receives gps data per second), everything is in order. But when I try to integrate these two modules - GPS stops working; it looks like the listener is not receiving any data, and the more that I am trying to use it on an emulator, so for this reason I initialized the latitude and longitude with a value so that in case the location is not received, it does not give null.
The application should function in such a way that as soon as the photo has been clicked and saved on the SD card, at the same time I should get the location of the gps device, which will also be required to save on the SD card. Have you had some similar problems?
The code is as follows:
public class MainActivity extends Activity implements CameraCallback{ private FrameLayout cameraholder = null; private CameraSurface camerasurface = null; LocationManager mLocationManager; LocationListener mlocListener; Double lat; Double lng; public class MyLocationListener implements LocationListener { public void onLocationChanged(Location loc) { String Text = "My current location is:\n" + "Latitude = " + loc.getLatitude() + "\nLongitude = " + loc.getLongitude()+ "\nAccuracy = "+ loc.getAccuracy(); Toast.makeText(MainActivity.this,Text,Toast.LENGTH_SHORT).show(); } public void onProviderDisabled(String provider) { Toast.makeText(MainActivity.this,"Gps Disabled",Toast.LENGTH_SHORT ).show(); } public void onProviderEnabled(String provider) { Toast.makeText(MainActivity.this,"Gps Enabled",Toast.LENGTH_SHORT).show(); } public void onStatusChanged(String provider, int status, Bundle extras){} { Toast.makeText(MainActivity.this, "Provider status changed",Toast.LENGTH_LONG).show(); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cameraholder = (FrameLayout)findViewById(R.id.camera_preview); mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); mlocListener = new MyLocationListener(); mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener); setupPictureMode(); ((ImageButton)findViewById(R.id.takepicture)).setOnClickListener(onButtonClick); ((ImageButton)findViewById(R.id.about)).setOnClickListener(onButtonClick); } private void setupPictureMode(){ camerasurface = new CameraSurface(this); cameraholder.addView(camerasurface, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); camerasurface.setCallback(this); } @Override public void onJpegPictureTaken(byte[] data, Camera camera) { try { long currentTime = System.currentTimeMillis(); FileOutputStream outStream = new FileOutputStream(String.format( "/sdcard/%d.jpg",currentTime)); outStream.write(data); outStream.close();
CameraPreview code looks like this:
public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback, OnGestureListener{ private Camera camera = null; private SurfaceHolder holder = null; private CameraCallback callback = null; private GestureDetector gesturedetector = null; public CameraSurface(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initialize(context); } public CameraSurface(Context context) { super(context); initialize(context); } public CameraSurface(Context context, AttributeSet attrs) { super(context, attrs); initialize(context); } private void initialize(Context context) { holder = getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); gesturedetector = new GestureDetector(this); } public void setCallback(CameraCallback callback){ this.callback = callback; } public void startPreview(){ camera.startPreview(); } public void startTakePicture(){ camera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { takePicture(); } }); } public void takePicture() { camera.takePicture( new ShutterCallback() { @Override public void onShutter(){ if(null != callback) callback.onShutter(); } }, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera){ if(null != callback) callback.onRawPictureTaken(data, camera); } }, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera){ if(null != callback) callback.onJpegPictureTaken(data, camera); } }); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) { if(null != camera) { camera.startPreview(); } } @Override public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); try { camera.setPreviewDisplay(holder); camera.setPreviewCallback(new Camera.PreviewCallback() { @Override public void onPreviewFrame(byte[] data, Camera camera) { if(null != callback) callback.onPreviewFrame(data, camera); } }); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; }}
The manifest file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="com.varma.samples.camera.ui.MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="10" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus"/> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Any help would be appreciated.