GPS местоположение тегов фотографий в Android

введите описание изображения здесь Я пытаюсь создать приложение, чтобы делать фотографии с помощью камеры мобильного телефона, сохранять их, прикреплять GPS-координаты к захваченному изображению. Я могу сделать снимок, сохранить его в альбом мобильного телефона, но не могу отобразить местоположение GPS (Lat,Lon) в полоске на изображении. Я использую exif, но не уверен, что я прав. Не могли бы вы посоветовать мне, как отображать координаты на моем изображении?

public class MainActivity extends AppCompatActivity {

ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File folder = new File(Environment.getExternalStorageDirectory() + "/PhotoGPSApp");
    if (folder.exists() == false) {
        folder.mkdirs();
    }

    Button btnCamera = (Button) findViewById(R.id.btnCamera);
    imageView = (ImageView) findViewById(R.id.imageView);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/PhotoGPSApp/Attachment" + ".jpg")));
            startActivityForResult(intent, 0);

        }
    });
}

LocationManager locationManager;

boolean gotLocation = false;

double longitude = 0.0;
double latitude = 0.0;

public boolean validLatLng (double lat, double lng) {
    if(lat != 0.0 && lng != 0.0){
        this.gotLocation = true;
        return true;
    } else return false;
}

public boolean haveLocation() {
    return this.gotLocation;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(bitmap);

    if (resultCode == RESULT_OK) {

        // Get LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        locationManager.requestLocationUpdates(provider, 1000, 0.0f, mLocationListener); // (String) provider, time in milliseconds when to check for an update, distance to change in coordinates to request an update, LocationListener.


    }

}

LocationListener mLocationListener = new LocationListener() {
    public void onLocationChanged (Location location){
        if (!haveLocation() && validLatLng(location.getLatitude(), location.getLongitude())) {
            //System.out.println("got new location");
            //Log.i("mLocationListener", "Got location");   // for logCat should ->  import android.util.Log;

            // Stops the new update requests.
            locationManager.removeUpdates(mLocationListener);
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            File f = new File(Environment.getExternalStorageDirectory(), "/PhotoGPSApp/Attachment" + ".jpg");
            geoTag(f.getAbsolutePath(), latitude, longitude);

        }
    }

    public void onStatusChanged(java.lang.String s, int i, android.os.Bundle bundle) {
    }

    public void onProviderEnabled(java.lang.String s){
    }

    public void onProviderDisabled(java.lang.String s){
    }

};



public void geoTag(String filename, double latitude, double longitude){
    ExifInterface exif;

    try {
        exif = new ExifInterface(filename);
        int num1Lat = (int)Math.floor(latitude);
        int num2Lat = (int)Math.floor((latitude - num1Lat) * 60);
        double num3Lat = (latitude - ((double)num1Lat+((double)num2Lat/60))) * 3600000;

        int num1Lon = (int)Math.floor(longitude);
        int num2Lon = (int)Math.floor((longitude - num1Lon) * 60);
        double num3Lon = (longitude - ((double)num1Lon+((double)num2Lon/60))) * 3600000;

        exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000");
        exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000");


        if (latitude > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
        }

        if (longitude > 0) {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
        } else {
            exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
        }

        exif.saveAttributes();

    } catch (IOException e) {
        Log.e("PictureActivity", e.getLocalizedMessage());
    }

}
   }

Мой код_действия.xml:

<ImageView
    android:id="@+id/imageView"
    android:layout_weight="9"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" />

<Button
    android:id="@+id/btnCamera"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:text="Open Camera"
    />

1 ответ

Задача решена. Я использовал textView.setText с GPS-координатами и обновил свой файл activity_main.xml для отображения в текстовой форме.

Другие вопросы по тегам