制作一个应从拍摄的照片中检索exif数据并将其存储在Room数据库中的应用。下面提供了一个代码,用于检索图片的纬度和经度以及日期。以下构造函数用于创建要存储在数据库中的对象。关键是该代码在手机上可以完美地工作(位置和日期都可以检索),但是仅在位置数据模拟器上(仍然可以检索日期)不能在模拟器上使用。调用getLatLong()
方法将返回null。
public Picture(String picturePath, String title) {
this.picturePath = picturePath;
this.title = title;
try {
ExifInterface exifInterface = new ExifInterface(picturePath);
double latLong[] = exifInterface.getLatLong();
this.lat = latLong != null ? latLong[0] : 0;
this.lon = latLong != null ? latLong[1] : 0;
this.date = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
} catch (IOException e) {
e.printStackTrace();
}
}
我仔细检查了所有权限,以及照片是否包含位置数据。授予了所有必要的权限,并且照片具有其位置。 Inside the app, the location is 0.0 for both lat and long。 However, looking at the picture details, we can see that there is a location of 40.000 10.000
我还设法使用getAttribute()
而不是android.media.ExifInterface
而不是android.support.media.ExifInterface
在模拟器上检索位置;重复一遍,这在电话上有效,但仅在模拟器上无效。
我的gradle文件也具有com.android.support:exifinterface:28.0.0
的依赖项。模拟器上的Android版本分别为9.0.0和8.0.0。
更新 使用EasyImage获取所有图像。这是我在MainActivity.java中所做的
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
EasyImage.handleActivityResult(requestCode, resultCode, data, this, new DefaultCallback() {
@Override
public void onImagePickerError(Exception e, EasyImage.ImageSource source, int type) {
//Some error handling
e.printStackTrace();
}
@Override
public void onImagesPicked(@NonNull List<File> imageFiles, EasyImage.ImageSource source, int type) {
onPhotosReturned(imageFiles);
adapter.onPhotosReturned(imageFiles, recyclerView);
}
@Override
public void onCanceled(EasyImage.ImageSource source, int type) {
//Cancel handling, you might wanna remove taken photo if it was canceled
if (source == EasyImage.ImageSource.CAMERA) {
File photoFile = EasyImage.lastlyTakenButCanceledPhoto(MainActivity.this);
if (photoFile != null) photoFile.delete();
}
}
});
}
private void onPhotosReturned(List<File> returnedPhotos) {
addImageElements(returnedPhotos);
}
private void addImageElements(List<File> returnedPhotos) {
for (File file : returnedPhotos) {
Picture element = new Picture(file.getAbsolutePath(), file.getName());
viewModel.getRepository().insert(element);
}
}
在PictureAdapter.java中
private List<Picture> pictures = new ArrayList<>();
private static List<Picture> allPictures = new ArrayList<>();
void setPictures(List<Picture> pictures) {
this.pictures = pictures;
notifyDataSetChanged();
}
static void setAllPictures(List<Picture> allPictures) {
PictureAdapter.allPictures = allPictures;
}
void onPhotosReturned(List<File> returnedPhotos, RecyclerView recyclerView) {
pictures.addAll(getImageElements(returnedPhotos));
setPictures(pictures);
}
private List<Picture> getImageElements(List<File> returnedPhotos) {
List<Picture> imageElementList = new ArrayList<>();
for (File file : returnedPhotos) {
Picture element = new Picture(file.getAbsolutePath(), file.getName());
imageElementList.add(element);
}
return imageElementList;
}