102 lines
3.9 KiB
Java
102 lines
3.9 KiB
Java
|
package com.fikyb.camloc;
|
||
|
|
||
|
import androidx.appcompat.app.AppCompatActivity;
|
||
|
|
||
|
import android.content.pm.PackageManager;
|
||
|
import android.os.Bundle;
|
||
|
|
||
|
import android.content.Intent;
|
||
|
import android.graphics.Bitmap;
|
||
|
import android.provider.MediaStore;
|
||
|
import android.widget.Button;
|
||
|
import android.widget.ImageView;
|
||
|
|
||
|
import android.Manifest;
|
||
|
import android.widget.TextView;
|
||
|
|
||
|
import androidx.core.app.ActivityCompat;
|
||
|
import com.google.android.gms.location.FusedLocationProviderClient;
|
||
|
import com.google.android.gms.location.LocationServices;
|
||
|
|
||
|
import java.text.SimpleDateFormat;
|
||
|
import java.util.Date;
|
||
|
|
||
|
public class MainActivity extends AppCompatActivity {
|
||
|
|
||
|
private Button btnCapture;
|
||
|
private ImageView imgCapturedImage;
|
||
|
private static final int REQUEST_IMAGE_CAPTURE = 1;
|
||
|
private FusedLocationProviderClient fusedLocationClient;
|
||
|
double globalLatitude;
|
||
|
double globalLongitude;
|
||
|
|
||
|
@Override
|
||
|
protected void onCreate(Bundle savedInstanceState) {
|
||
|
super.onCreate(savedInstanceState);
|
||
|
setContentView(R.layout.activity_main);
|
||
|
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
|
||
|
|
||
|
btnCapture = findViewById(R.id.btnTakePicture);
|
||
|
imgCapturedImage = findViewById(R.id.imageView);
|
||
|
|
||
|
TextView textView = findViewById(R.id.textViewDescription);
|
||
|
textView.setText(fusedLocationClient.toString());
|
||
|
btnCapture.setOnClickListener(v -> {
|
||
|
dispatchTakePictureIntent();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void dispatchTakePictureIntent() {
|
||
|
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||
|
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
|
||
|
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||
|
super.onActivityResult(requestCode, resultCode, data);
|
||
|
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
|
||
|
Bundle extras = data.getExtras();
|
||
|
Bitmap imageBitmap = (Bitmap) extras.get("data");
|
||
|
imgCapturedImage.setImageBitmap(imageBitmap);
|
||
|
}
|
||
|
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
|
||
|
Bundle extras = data.getExtras();
|
||
|
Bitmap imageBitmap = (Bitmap) extras.get("data");
|
||
|
imgCapturedImage.setImageBitmap(imageBitmap);
|
||
|
|
||
|
// Get timestamp
|
||
|
long timestamp = extras.getLong("android.media.extra.TIMESTAMP");
|
||
|
|
||
|
// Format timestamp
|
||
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
|
String dateString = formatter.format(new Date(timestamp));
|
||
|
|
||
|
// Combine location and date information
|
||
|
String description = "Taken at " + dateString + " (" + globalLatitude + ", " + globalLongitude + ")";
|
||
|
|
||
|
// Show description above the image
|
||
|
TextView textView = findViewById(R.id.textViewDescription);
|
||
|
textView.setText(description);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
private void getLocation() {
|
||
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
|
||
|
return;
|
||
|
}
|
||
|
fusedLocationClient.getLastLocation()
|
||
|
.addOnSuccessListener(this, location -> {
|
||
|
if (location != null) {
|
||
|
// Get latitude and longitude
|
||
|
globalLatitude = location.getLatitude();
|
||
|
globalLongitude = location.getLongitude();
|
||
|
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|