title: Getting location data for a taken photo in Android
body: <p>I don't know how to get the location (GPS/Network when taking a photo) in Android. 
  Any suggestion and code samples are welcomed. I haven't used until now the Location services, I have tried in many ways but I can't get it to work.</p>
  
  <p>Here is my image capturing activity:</p>
  
  <pre><code>public class ImageCaptureActivity extends Activity {
  
  // Activity request codes
  private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
  public static final int MEDIA_TYPE_IMAGE = 1;
  Context context = null;
  
  // directory name to store captured images
  private static final String IMAGE_DIRECTORY_NAME = "Asisoft";
  
  private Uri fileUri; // file url to store image
  
  private ImageView imgPreview;
  private Button btnCapturePicture;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.image_capture);
  
      context = getApplicationContext();
      imgPreview = (ImageView) findViewById(R.id.imgPreview);
      btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
  
      /**
       * Capture image button click event
       * */
      btnCapturePicture.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // capture picture
              captureImage();
          }
      });
  
  }
  </code></pre>
  
  <p>Receiving activity result method will be called after closing the camera</p>
  
  <pre><code>@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      // if the result is capturing Image
      if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
          if (resultCode == RESULT_OK) {
              // successfully captured the image
              // display it in image view
              previewCapturedImage();
  
          } else if (resultCode == RESULT_CANCELED) {
              // user cancelled Image capture
              Toast.makeText(getApplicationContext(),
                      "Cancelled", Toast.LENGTH_SHORT)
                      .show();
          } else {
              // failed to capture image
              Toast.makeText(getApplicationContext(),
                      "Error!", Toast.LENGTH_SHORT)
                      .show();
          }
      }
  
  }
  </code></pre>
  
  <p>Here we store the file url as it will be null after returning from camera app</p>
  
  <pre><code>@Override
  protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
  
      // save file url in bundle as it will be null on scren orientation
      // changes
      outState.putParcelable("file_uri", fileUri);
  }
  </code></pre>
  
  <p>Here I restore the fileUri again</p>
  
  <pre><code>@Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
  
      // get the file url
      fileUri = savedInstanceState.getParcelable("file_uri");
  }
  </code></pre>
  
  <p>Method to fire up camera</p>
  
  <pre><code>private void captureImage() {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  
      fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
  
      intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  
      // start the image capture Intent
      startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
  
  }
  </code></pre>
  
  <p>Creating file uri to store image/video</p>
  
  <pre><code>public Uri getOutputMediaFileUri(int type) {
      return Uri.fromFile(getOutputMediaFile(type));
  }
  
  /*
   * returning image / video
   */
  private static File getOutputMediaFile(int type) {
  
      // External sdcard location
      File mediaStorageDir = new File(
              Environment
                      .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
              IMAGE_DIRECTORY_NAME);
  
      // Create the storage directory if it does not exist
      if (!mediaStorageDir.exists()) {
          if (!mediaStorageDir.mkdirs()) {
              Log.d(IMAGE_DIRECTORY_NAME, "Fisierul "
                      + IMAGE_DIRECTORY_NAME + " nu a fost creat");
              return null;
          }
      }
  
      // Create a media file name
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
              Locale.getDefault()).format(new Date());
      File mediaFileName;
      if (type == MEDIA_TYPE_IMAGE) {
          mediaFileName = new File(mediaStorageDir.getPath() + File.separator
                  + "IMG_" + timeStamp + ".jpg");
      } else {
          return null;
      }
  
      return mediaFileName;
  }
  </code></pre>
  
  <p>Display image from a path to ImageView</p>
  
  <pre><code>private void previewCapturedImage() {
      try {
          imgPreview.setVisibility(View.VISIBLE);
  
          // bimatp factory
          BitmapFactory.Options options = new BitmapFactory.Options();
  
          // downsizing image as it throws OutOfMemory Exception for larger
          // images
          options.inSampleSize = 8;
  
          final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                  options);
  
          imgPreview.setImageBitmap(bitmap);
  
      } catch (NullPointerException e) {
          e.printStackTrace();
      }
  }
  
  
  }
  </code></pre>