2012-03-22 198 views
1

当我拍照时,我试图获得gps位置。当LocationChanged方法在变量中运行时,我保存了位置侦听器的位置,并希望在拍摄照片时显示它。这是正确的方式吗?我使用android 2.3.6。 这里我的代码:拍摄照片后获得gps位置

package com.exercise; 



import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.OutputStream; 

import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.pm.ActivityInfo; 
import android.graphics.PixelFormat; 
import android.hardware.Camera; 
import android.hardware.Camera.AutoFocusCallback; 
import android.hardware.Camera.PictureCallback; 
import android.hardware.Camera.ShutterCallback; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore.Images.Media; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.Toast; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 

public class AndroidCamera extends Activity implements SurfaceHolder.Callback{ 

    Camera camera; 
    SurfaceView surfaceView; 
    SurfaceHolder surfaceHolder; 
    boolean previewing = false; 
    LayoutInflater controlInflater = null; 

    Button buttonTakePicture; 

    final int RESULT_SAVEIMAGE = 0; 

    public Location test; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

     LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

     getWindow().setFormat(PixelFormat.UNKNOWN); 
     surfaceView = (SurfaceView)findViewById(R.id.camerapreview); 
     surfaceHolder = surfaceView.getHolder(); 
     surfaceHolder.addCallback(this); 
     surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

     controlInflater = LayoutInflater.from(getBaseContext()); 
     View viewControl = controlInflater.inflate(R.layout.control, null); 
     LayoutParams layoutParamsControl 
      = new LayoutParams(LayoutParams.FILL_PARENT, 
      LayoutParams.FILL_PARENT); 
     this.addContentView(viewControl, layoutParamsControl); 

     buttonTakePicture = (Button)findViewById(R.id.takepicture); 
     buttonTakePicture.setOnClickListener(new Button.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       camera.takePicture(myShutterCallback, 
         myPictureCallback_RAW, myPictureCallback_JPG); 
      }}); 

     LinearLayout layoutBackground = (LinearLayout)findViewById(R.id.background); 
     layoutBackground.setOnClickListener(new LinearLayout.OnClickListener(){ 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

       buttonTakePicture.setEnabled(false); 
       camera.autoFocus(myAutoFocusCallback); 
      }}); 
    } 

    public class MyLocationListener implements LocationListener{ 

     @Override 

     public void onLocationChanged(Location loc){ 
      loc.getLatitude(); 
      loc.getLongitude(); 
      String Text = "Lat = " + loc.getLatitude() + "|Long = " + loc.getLongitude(); 
      Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 
      // final TextView tv = (TextView) findViewById(R.id.text); 
      // tv.setText(tv.getText()+ "||" + Text); 
      test = loc; 
     } 

     @Override 

     public void onProviderDisabled(String provider){ 
      Toast.makeText(getApplicationContext(),"Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 

     @Override 

     public void onProviderEnabled(String provider){ 
      Toast.makeText(getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show(); 
     } 

     @Override 

     public void onStatusChanged(String provider, int status, Bundle extras){ 
     } 

    } 

    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){ 

     @Override 
     public void onAutoFocus(boolean arg0, Camera arg1) { 
      // TODO Auto-generated method stub 
      buttonTakePicture.setEnabled(true); 
     }}; 

    ShutterCallback myShutterCallback = new ShutterCallback(){ 

     @Override 
     public void onShutter() { 
      // TODO Auto-generated method stub 

     }}; 

    PictureCallback myPictureCallback_RAW = new PictureCallback(){ 

     @Override 
     public void onPictureTaken(byte[] arg0, Camera arg1) { 
      // TODO Auto-generated method stub 

     }}; 

    PictureCallback myPictureCallback_JPG = new PictureCallback(){ 

     @Override 
     public void onPictureTaken(byte[] arg0, Camera arg1) { 
      // TODO Auto-generated method stub 
      /*Bitmap bitmapPicture 
       = BitmapFactory.decodeByteArray(arg0, 0, arg0.length); */ 

      Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues()); 

      OutputStream imageFileOS; 
      try { 
       imageFileOS = getContentResolver().openOutputStream(uriTarget); 
       imageFileOS.write(arg0); 
       imageFileOS.flush(); 
       imageFileOS.close(); 

//    Toast.makeText(AndroidCamera.this, 
//      "Image saved: " + uriTarget.toString(), 
//      Toast.LENGTH_LONG).show(); 
//    test.getLatitude(); 
//    test.getLongitude(); 
//    String Text = "Lat = " + test.getLatitude() + "|Long = " + test.getLongitude(); 
//    Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 
       if (test==null){ 
        Toast.makeText(AndroidCamera.this, "null", Toast.LENGTH_SHORT).show(); 
       } 
       else{ 
        test.getLatitude(); 
        String ausgabe = String.valueOf(test.getLatitude()); 
        Toast.makeText(AndroidCamera.this, ausgabe, Toast.LENGTH_SHORT).show(); 
       } 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      camera.startPreview(); 
     }}; 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // TODO Auto-generated method stub 
     if(previewing){ 
      camera.stopPreview(); 
      previewing = false; 
     } 

     if (camera != null){ 
      try { 
       camera.setPreviewDisplay(surfaceHolder); 
       camera.startPreview(); 
       previewing = true; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     camera = Camera.open(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     camera.stopPreview(); 
     camera.release(); 
     camera = null; 
     previewing = false; 
    } 
} 

回答

1

是的,这是一个公平的方式来做到这一点。顺便说一下,您可以改为.requestSingleUpdate(),因为您只需要一次更新。你也应该等到GPS信息到达时(也可以指定与GPS提供者不同的东西,比如任何提供者,这可能会更快地响应 - 而不是GPS可能的冷启动)。如果这些都不起作用,则始终可以查询最后一个已知位置。由于更新可能暂时不会进行,因此您应该将图像数据暂时保存在某个位置(没有位置信息),然后在位置可用时更新信息。

+0

但我怎样才能得到的位置在照片的时刻,我如何激活搜索的位置? – user1284946 2012-03-23 00:43:54

+0

你不能。如果GPS启动,它可能需要一段时间才能预热并修复该位置。如果它尚未解决问题,那么在启动之前无法知道您的位置。相反,当您拍摄照片时,您会开始并请求更新,然后几秒/分钟后,希望您可以修复该位置。没有什么神奇的方法可以立即获得位置。 – 2012-03-23 02:17:37