2017-09-13 1 views
1

Ich versuche, Google Location-Daten zum EXIF-Header der Bilder hinzuzufügen, die in meiner Anwendung aufgenommen wurden. Bis jetzt bin ich in der Lage, den Standort unter Verwendung fused location provider zu erhalten und es in der MainActivity zu rösten. Aber wenn ich versuche, die Positionsdaten in den EXIF-Header der Bilder zu schreiben, die in einem Thread aufgenommen wurden, kann ich das Location-Objekt dort nicht finden. Es zeigt 'Cannot resolve symbol 'location'. Jede Hilfe wird geschätzt.Symbol kann nicht aufgelöst werden. Es konnten keine Standortdaten zu EXIF ​​eines Bildes hinzugefügt werden.

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

try Ihren empfangenen Standort in onSuccess zu einem globalen Attribute zu übergeben und Ihr globales Attribut in markGeoTagImage – MHP

+0

Wie verwenden 'public static String = null;' in der Aktivität ?? Es wäre großartig, wenn Sie es im Code erklären. –

+0

Ich poste die Antwort für Sie, Sie sollten das Objekt vom Typ Location definieren und es in onSuccess initialisieren, wenn die Position – MHP

Antwort

1

neues zum Beispiel globales Attribut definieren mLocation und erhielt location es passieren und es wie folgt verwenden:

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(); 

und dann mLocation in Ihrem EXIF ​​verwendet, wie folgt aus:

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

Es zeigt immer noch nicht auflösen. Mein Thread ist in einer anderen Klasse. –

+0

so übergeben Sie mlocation zu saveBitmap in einer Weise – MHP

+1

Problem gelöst. Ich habe es nur "öffentliche statische Location mlocation" gemacht. Dann kam es dort im Thread. Bitte aktualisieren Sie die Antwort. Ich werde es abstimmen. Danke vielmals . –

Verwandte Themen