Aktuelle GPS Position bei Photoaufnahme

  • Antworten:1
Eisbar
  • Forum-Beiträge: 2

22.03.2012, 01:48:06 via Website

Hallo ich brauche die aktuelle GPS Position bei der Aufnahme eines Fotos. Hierzu verwende ich den LocationListener und speichere die Location in einer Variablen auf der ich beim Photomachen zurückgreife, jedoch wird keine Ausgabe gemacht. Was mache ich falsch?

1package com.exercise;
2
3
4
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.OutputStream;
8
9import android.app.Activity;
10import android.content.ContentValues;
11import android.content.Context;
12import android.content.pm.ActivityInfo;
13import android.graphics.PixelFormat;
14import android.hardware.Camera;
15import android.hardware.Camera.AutoFocusCallback;
16import android.hardware.Camera.PictureCallback;
17import android.hardware.Camera.ShutterCallback;
18import android.net.Uri;
19import android.os.Bundle;
20import android.provider.MediaStore.Images.Media;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.SurfaceHolder;
24import android.view.SurfaceView;
25import android.view.View;
26import android.view.ViewGroup.LayoutParams;
27import android.widget.Button;
28import android.widget.LinearLayout;
29import android.widget.Toast;
30import android.location.Location;
31import android.location.LocationListener;
32import android.location.LocationManager;
33
34public class AndroidCamera extends Activity implements SurfaceHolder.Callback{
35
36 Camera camera;
37 SurfaceView surfaceView;
38 SurfaceHolder surfaceHolder;
39 boolean previewing = false;
40 LayoutInflater controlInflater = null;
41
42 Button buttonTakePicture;
43
44 final int RESULT_SAVEIMAGE = 0;
45
46 public Location test;
47
48 /** Called when the activity is first created. */
49 @Override
50 public void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
52 setContentView(R.layout.main);
53 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
54
55 LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
56 LocationListener mlocListener = new MyLocationListener();
57 mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
58
59 getWindow().setFormat(PixelFormat.UNKNOWN);
60 surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
61 surfaceHolder = surfaceView.getHolder();
62 surfaceHolder.addCallback(this);
63 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
64
65 controlInflater = LayoutInflater.from(getBaseContext());
66 View viewControl = controlInflater.inflate(R.layout.control, null);
67 LayoutParams layoutParamsControl
68 = new LayoutParams(LayoutParams.FILL_PARENT,
69 LayoutParams.FILL_PARENT);
70 this.addContentView(viewControl, layoutParamsControl);
71
72 buttonTakePicture = (Button)findViewById(R.id.takepicture);
73 buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
74
75 @Override
76 public void onClick(View arg0) {
77 // TODO Auto-generated method stub
78 camera.takePicture(myShutterCallback,
79 myPictureCallback_RAW, myPictureCallback_JPG);
80 }});
81
82 LinearLayout layoutBackground = (LinearLayout)findViewById(R.id.background);
83 layoutBackground.setOnClickListener(new LinearLayout.OnClickListener(){
84
85 @Override
86 public void onClick(View arg0) {
87 // TODO Auto-generated method stub
88
89 buttonTakePicture.setEnabled(false);
90 camera.autoFocus(myAutoFocusCallback);
91 }});
92 }
93
94 public class MyLocationListener implements LocationListener{
95
96 @Override
97
98 public void onLocationChanged(Location loc){
99 loc.getLatitude();
100 loc.getLongitude();
101 String Text = "Lat = " + loc.getLatitude() + "|Long = " + loc.getLongitude();
102 Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
103 // final TextView tv = (TextView) findViewById(R.id.text);
104 // tv.setText(tv.getText()+ "||" + Text);
105 test = loc;
106 }
107
108 @Override
109
110 public void onProviderDisabled(String provider){
111 Toast.makeText( getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT ).show();
112 }
113
114 @Override
115
116 public void onProviderEnabled(String provider){
117 Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
118 }
119
120 @Override
121
122 public void onStatusChanged(String provider, int status, Bundle extras){
123 }
124
125 }
126
127 AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){
128
129 @Override
130 public void onAutoFocus(boolean arg0, Camera arg1) {
131 // TODO Auto-generated method stub
132 buttonTakePicture.setEnabled(true);
133 }};
134
135 ShutterCallback myShutterCallback = new ShutterCallback(){
136
137 @Override
138 public void onShutter() {
139 // TODO Auto-generated method stub
140
141 }};
142
143 PictureCallback myPictureCallback_RAW = new PictureCallback(){
144
145 @Override
146 public void onPictureTaken(byte[] arg0, Camera arg1) {
147 // TODO Auto-generated method stub
148
149 }};
150
151 PictureCallback myPictureCallback_JPG = new PictureCallback(){
152
153 @Override
154 public void onPictureTaken(byte[] arg0, Camera arg1) {
155 // TODO Auto-generated method stub
156 /*Bitmap bitmapPicture
157 = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */
158
159 Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
160
161 OutputStream imageFileOS;
162 try {
163 imageFileOS = getContentResolver().openOutputStream(uriTarget);
164 imageFileOS.write(arg0);
165 imageFileOS.flush();
166 imageFileOS.close();
167
168// Toast.makeText(AndroidCamera.this,
169// "Image saved: " + uriTarget.toString(),
170// Toast.LENGTH_LONG).show();
171// test.getLatitude();
172// test.getLongitude();
173// String Text = "Lat = " + test.getLatitude() + "|Long = " + test.getLongitude();
174// Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
175 if (test==null){
176 Toast.makeText(AndroidCamera.this, "null", Toast.LENGTH_SHORT);
177 }
178 else{
179 test.getLatitude();
180 String ausgabe = String.valueOf(test.getLatitude());
181 Toast.makeText(AndroidCamera.this, ausgabe, Toast.LENGTH_SHORT);
182 }
183
184 } catch (FileNotFoundException e) {
185 // TODO Auto-generated catch block
186 e.printStackTrace();
187 } catch (IOException e) {
188 // TODO Auto-generated catch block
189 e.printStackTrace();
190 }
191
192 camera.startPreview();
193 }};
194
195 @Override
196 public void surfaceChanged(SurfaceHolder holder, int format, int width,
197 int height) {
198 // TODO Auto-generated method stub
199 if(previewing){
200 camera.stopPreview();
201 previewing = false;
202 }
203
204 if (camera != null){
205 try {
206 camera.setPreviewDisplay(surfaceHolder);
207 camera.startPreview();
208 previewing = true;
209 } catch (IOException e) {
210 // TODO Auto-generated catch block
211 e.printStackTrace();
212 }
213 }
214 }
215
216 @Override
217 public void surfaceCreated(SurfaceHolder holder) {
218 // TODO Auto-generated method stub
219 camera = Camera.open();
220 }
221
222 @Override
223 public void surfaceDestroyed(SurfaceHolder holder) {
224 // TODO Auto-generated method stub
225 camera.stopPreview();
226 camera.release();
227 camera = null;
228 previewing = false;
229 }
230}

Antworten
Eisbar
  • Forum-Beiträge: 2

22.03.2012, 05:35:44 via Website

also mittlerweile bekomme ich eine Ausgabe aber das Objekt ist "null". GPS ist an und die Rechte habe ich auch gesetzt.

Antworten