gpt4 book ai didi

java - 我的应用程序使用相机不记录纬度和经度(文件元数据)

转载 作者:行者123 更新时间:2023-12-02 09:04:11 25 4
gpt4 key购买 nike

...但是当使用标准相机时,它可以完美地工作。

我正在努力编写一个代码,它允许我在 fragment 中拍照,也允许我检查它在 map (另一个 fragment )中使用 de LatLng 进行预览,并带有两个显示纬度和经度的标签。

我得出的结论是,问题一定出在拍照时,因为当我从图库中检索标准相机照片时,我可以看到纬度和经度。使用我的应用创建的文件确实如此根本没有地理定位。

我尝试了不同的权限。我检查文件类型JPG我检查 android map 的准确性我但是有一个奇怪的问题“如果我重置我的模拟器手机并在第一次相机拍摄正常工作后启动”

The left one represents a Photo made by my app(NO LatLan) the other one with the standard  data is in it.

'''

公共(public)类 HomeFragment 扩展 Fragment {

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 获取纬度和经度值并将其作为元值写入文件,如 show下面

     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 - 我的应用程序使用相机不记录纬度和经度(文件元数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59959150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com