Java 使用摄像头的我的应用程序不记录纬度和经度(文件元数据)
。。。。但当你使用标准相机时,它工作得非常完美 我正在与一个允许我在片段中拍照并允许我检查的代码作斗争 这是一个地图(另一个片段)中的delatlng预览,有两个标签显示lat和longJava 使用摄像头的我的应用程序不记录纬度和经度(文件元数据),java,android,camera,latitude-longitude,exif,Java,Android,Camera,Latitude Longitude,Exif,。。。。但当你使用标准相机时,它工作得非常完美 我正在与一个允许我在片段中拍照并允许我检查的代码作斗争 这是一个地图(另一个片段)中的delatlng预览,有两个标签显示lat和long 我得出的结论是,问题一定出在拍照时,因为当我从图库中检索标准相机的照片时,我可以看到Lat和long。使用我的应用程序创建的文件根本没有地理定位 我尝试了不同的权限。 我检查JPG文件的类型 我检查android地图的准确性 但有一个奇怪的问题,“如果我重置我的模拟器手机,并在第一次拍摄时正常启动” '''
我得出的结论是,问题一定出在拍照时,因为当我从图库中检索标准相机的照片时,我可以看到Lat和long。使用我的应用程序创建的文件根本没有地理定位 我尝试了不同的权限。 我检查JPG文件的类型 我检查android地图的准确性 但有一个奇怪的问题,“如果我重置我的模拟器手机,并在第一次拍摄时正常启动” ''' 公共类HomeFragment扩展了片段{
private final String TAG = "HomeFragment";
//
private Uri uriSavedImage;
private ImageView imageView;
private Button btn_takePhoto;
private static final int CAPTURA_RQC = 1;
private static final int REQUEST_PERMISION_CODE = 2;
private View root;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
imageView = root.findViewById(R.id.imageView2);
//
btn_takePhoto = root.findViewById(R.id.button);
btn_takePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//checkPermissions
if (checkPermissions()) {
takePhoto();
} else {
askForPermissions();
}
}
});
return root;
}
public void takePhoto() {
//takePhoto
//Allows access otherwise...
/// android.os.FileUriExposedException: file:///sdcard/DCIM/Camera/... .png exposed beyond app through ClipData.Item.getUri()
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
//Crear el intent
Intent hacerFotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Create unique name.
long timeStamp = System.currentTimeMillis();
/**
* I think the problem is around here.
*/
//Different path taken form emulator
String pathname = "/sdcard/DCIM/Camera/image_" + timeStamp + ".JPG";
// String pathname = "/sdcard/file_example.png";
//
uriSavedImage = Uri.fromFile(new File(pathname));
//
hacerFotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(hacerFotoIntent, CAPTURA_RQC);
}
public void fetchImage( ) {
File imgFile = new File(uriSavedImage.getPath());
if (imgFile.exists()) {//
try {
Bitmap mBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uriSavedImage);
imageView.setImageBitmap(mBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURA_RQC && resultCode == Activity.RESULT_OK) {
fetchImage( );
} else {
Toast toast = Toast.makeText(getActivity(), "Could not find the image ", Toast.LENGTH_SHORT);
toast.show();
}
}
/**
* @return
*/
public boolean checkPermissions() {
Log.i(TAG, "checkPermissions");
int permission_read = ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(),
READ_EXTERNAL_STORAGE);
int permission_write_sd = ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
WRITE_EXTERNAL_STORAGE);
int permission_access_camera = ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
CAMERA);
int permission_FINE_LOCATION = ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
ACCESS_FINE_LOCATION);
int permission_COARSE_LOCATION = ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
ACCESS_COARSE_LOCATION);
int permission_INTERNT = ContextCompat.checkSelfPermission(getActivity().getApplicationContext(),
INTERNET);
return permission_write_sd == PackageManager.PERMISSION_GRANTED
&&
permission_read == PackageManager.PERMISSION_GRANTED
&&
permission_access_camera == PackageManager.PERMISSION_GRANTED
&&
permission_FINE_LOCATION == PackageManager.PERMISSION_GRANTED
&&
permission_COARSE_LOCATION == PackageManager.PERMISSION_GRANTED
&&
permission_INTERNT == PackageManager.PERMISSION_GRANTED
;
}
private void askForPermissions() {
ActivityCompat.requestPermissions(getActivity(),
new String[]{
CAMERA,
READ_EXTERNAL_STORAGE,
WRITE_EXTERNAL_STORAGE,
ACCESS_FINE_LOCATION,
ACCESS_COARSE_LOCATION,
ACCESS_FINE_LOCATION,
ACCESS_BACKGROUND_LOCATION
}
, REQUEST_PERMISION_CODE);
}
}
''从相机捕获图像后,当您将位图保存到文件位置时,您可以做的是从LocationServices获取lat和long值,并将其作为元数据写入文件,如下图所示
String pathname = "/sdcard/DCIM/Camera/image_" + timeStamp + ".JPG";
ExifInterface exif = new ExifInterface(pathname);
// call this next setAttributes a few times to write all the GPS data to it.
exif.setAttribute(... );
// don't forget to save
exif.saveAttributes();
您可以使用ExiFinInterface将需要查看的元数据写入该文件,稍后在查看照片的详细信息时需要查看这些元数据--“使用我的应用程序创建的文件根本没有地理定位”--您的应用程序没有创建任何文件。摄像头应用程序正在运行。该摄像头应用程序是用户使用的数百(如果不是数千)个摄像头应用程序中的一个,包括预安装和用户安装。无论出于何种原因,您正在使用的相机应用程序不会对通过
操作\u图像\u捕获拍摄的照片进行地理标记。可以说,这是一件好事,特别是当用户无法在相机用户界面的特定部分选择退出时。无论如何,这是相机应用开发者的决定。