我正在嘗試將Google位置數據添加到在我的應用程序中拍攝的圖像的EXIF標頭中。到目前爲止,我可以使用fused location provider獲取位置,並在MainActivity中對其進行烘烤。但是,當試圖將位置數據寫入線程中拍攝的圖像的EXIF標題時,我無法獲取位置對象。它顯示'Cannot resolve symbol 'location'
。任何幫助表示讚賞。無法解析符號。無法將位置數據添加到圖像的EXIF。
MainActivity.java
private TestThread myThread;
private FusedLocationProviderClient mFusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
System.loadLibrary("NativeImageProcessor");
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
// ...
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(MainActivity.this, "Latitude:" + latitude+", Longitude:"+longitude,Toast.LENGTH_SHORT).show();
}
}
});
...
...
takePicture();
TestThread.java
....
private void saveBitmap(String name, Bitmap bitmap, String timeStamp) {
File photo = CommonUtil.getFileName(timeStamp, name);
if (photo.exists()) photo.delete();
try {
Bitmap image = applyBrightnessContrast(bitmap);
FileOutputStream fos = new FileOutputStream(photo.getPath());
image.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
CommonUtil.showProgressDialog(mContext, null, false);
} catch (java.io.IOException e) {
CommonUtil.showProgressDialog(mContext, null, false);
Log.e("PictureDemo", "Exception in photoCallback", e);
}
markGeoTagImage(photo.getPath(),location);// Here is the Error.
}
public static void markGeoTagImage(String imagePath,Location location)
{
try {
ExifInterface exif = new ExifInterface(imagePath);
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, GPS.convert(location.getLatitude()));
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, GPS.latitudeRef(location.getLatitude()));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, GPS.convert(location.getLongitude()));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, GPS.longitudeRef(location.getLongitude()));
SimpleDateFormat fmt_Exif = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
exif.setAttribute(ExifInterface.TAG_DATETIME,fmt_Exif.format(new Date(location.getTime())));
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
}
嘗試在的onSuccess通過您收到的位置,以一個全球性的屬性,並使用您的全局屬性在markGeoTagImage – MHP
作爲「公共靜態字符串= NULL;」在活動中?如果你在代碼中解釋它會很好。 –
我爲你發佈答案,你應該定義從類型位置的對象,並初始化它在onSuccess當位置接收 – MHP