2017-09-13 70 views
1

我正在嘗試將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(); 
    } 
} 
+0

嘗試在的onSuccess通過您收到的位置,以一個全球性的屬性,並使用您的全局屬性在markGeoTagImage – MHP

+0

作爲「公共靜態字符串= NULL;」在活動中?如果你在代碼中解釋它會很好。 –

+0

我爲你發佈答案,你應該定義從類型位置的對象,並初始化它在onSuccess當位置接收 – MHP

回答

1

定義例如mLocation新的全球屬性,並通過接收到location它,使用它是這樣的:

public static Location mLocation; // this is your location 

@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) { 
         mLocation = location; // we pass received location to our global location object 
         // ... 
         double latitude = location.getLatitude(); 
         double longitude = location.getLongitude(); 
         Toast.makeText(MainActivity.this, "Latitude:" + latitude+", Longitude:"+longitude,Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
      ... 
      ... 
     takePicture(); 

,然後用mLocation在EXIF,像這樣:

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); 
     } 
    if(mLocation != null)// check we have location or not 
    markGeoTagImage(photo.getPath(),mLocation); 
} 
+0

它仍然顯示不解決。我的線程是在一個不同的類。 –

+0

因此通過mLocation以saveBitmap方式 – MHP

+1

問題解決。我只是讓它成爲'public static Location mLocation'。然後它在線程中來到了那裏。請更新答案。我會投票。非常感謝 。 –