java - 我使用相机的应用程序不记录纬度和经度(文件元数据)
问题描述
....但是当使用标准相机时,它可以完美地工作。
我正在努力反对允许我在片段中拍摄照片并允许我在地图(另一个片段)中使用 de LatLng 检查它的预览的代码,其中两个标签显示经度和经度。
我得出的结论是,问题一定出在拍照时,因为当我从图库中检索标准相机的照片时,我可以看到经纬度 。使用我的应用程序创建的文件根本没有地理定位。
我尝试了不同的权限。我检查文件的类型 JPG 我检查了 android 地图的准确性我但是有一个奇怪的问题“如果我重置我的 Emulator PHONE 并在它适用于第一个相机拍摄时启动”
'''
公共类 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();
您可以使用ExifInterface将您需要的任何元数据写入您需要稍后查看照片的详细信息时
推荐阅读
- java - Mockito 无法使用文件输入流模拟类
- oracle - Oracle PL/SQL 处理游标中未找到的值
- javascript - 使用翻译在 html/js 中选择国家
- php - PHP:如何获取 html 标签中的内容?
- java - 为什么我在 Scanner 中收到 java.util.NoSuchElementException 错误
- android - 谷歌地图中的“templateMergeStrategy”属性是什么?
- javascript - 从链接打开 iOS 笔记应用程序并粘贴剪贴板的内容
- git - jdeveloper 12c 中的 Git hub 插件
- javascript - 在 React 中更改 Radio 输入后,如何从其他嵌套的同级中删除 className?
- python - 函数中的“TypeError:'list'对象不可调用”