2017-08-08 224 views
0

我正在嘗試從攝像頭獲取拍攝圖片的位置。當我嘗試的Exif接口,但仍然photos.Please有人幫助返回空值...如何在Android中從攝像頭獲取當前拍攝圖片的位置

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
          startActivityForResult(i, 100); 
    @Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 100 && resultCode == RESULT_OK && data != null) { 
     // Let's read picked image data - its URI 
     Uri pickedImage = data.getData(); 
     // Let's read picked image path using content resolver 
     String[] filePath = {MediaStore.Images.Media.DATA}; 
     Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null); 
     cursor.moveToFirst(); 
     img1 = cursor.getString(cursor.getColumnIndex(filePath[0])); 


     ExifInterface exif = null; 
     try { 
      exif = new ExifInterface(img1); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     String lat = ExifInterface.TAG_GPS_LATITUDE; 
      String lat_data = exif.getAttribute(lat); 
      Log.e("MYDATA", lat_data); 
      cursor.close(); 
      Toast.makeText(this, "Image Uploaded", Toast.LENGTH_SHORT).show(); 

    } 
+0

分享你的代碼你試過的東西 – sumit

+0

驗證你的文件真的有位置數據,大多數時候android設備的功能在默認情況下禁用相機應用程序。 – smora

+0

其實我在Android設備中啓用地理定位。 –

回答

0

使用內容解析器隨同InputStream建立ExifInterface

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 
     if (resultCode != Activity.RESULT_OK) { 
      return; 
     } 
     final Uri originalUri = intent.getData(); 

     try { 
      final InputStream inputStream = getContext().getContentResolver().openInputStream(originalUri); 
      final android.support.media.ExifInterface exif = new android.support.media.ExifInterface(inputStream); 
      final double[] latLong = exif.getLatLong(); 
      Log.v(LOG_TAG, "Image selected at position: " + latLong[0] + " : " + latLong[1]); 

     } catch (IOException e) { 
      Log.w(LOG_TAG, "Error when getting location from image", e); 
     } 
    } 

支持的lib進口

compile 'com.android.support:exifinterface:x.x.x' 
+0

final double [] latLong = exif.getLatLong(????);在這個論據中應用了 –

+0

檢查文檔的api,方法在不同版本的api中發生了變化。使用支持版本導入android.support.media.ExifInterface; – smora

+0

ExifInterface exif = null; 嘗試exif = new ExifInterface(img1); catch(IOException e){ e.printStackTrace(); } String lat = ExifInterface.TAG_GPS_LATITUDE; 字符串lat_data = exif.getAttribute(lat); Log.e(「MYDATA」,lat_data); String log = ExifInterface.TAG_GPS_LONGITUDE; String log_data = exif.getAttribute(log); Log.e(「MYDATA1」,log_data);感謝我使用此代碼獲得解決方案 –

相關問題