2016-09-16 86 views
6

我問這個的原因是因爲文件選擇器Intent的回調返回Uri。通過意向如何從InputStream而不是文件獲取Exif數據?

打開文件選擇:

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Select Picture"), CHOOSE_IMAGE_REQUEST); 

回調:

@Override 
public void onActivityResult(int requestCode, int resultCode, final Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == CHOOSE_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) { 

     if (data == null) { 
      // Error 
      return; 
     } 

     Uri fileUri = data.getData(); 
     InputStream in = getContentResolver().openInputStream(fileUri); 

     // How to determine image orientation through Exif data here? 
    } 
} 

一種方式是寫InputStream到實際File,但是這似乎是一個不好的解決辦法我。

回答

8

在引入25.1.0支持庫之後,現在可以通過InputStream從URI內容(content://或file://)讀取exif數據。

例子: 首先這行添加到您的gradle這個文件:

編譯 'com.android.support:exifinterface:25.1.0'

Uri uri; // the URI you've received from the other app 
InputStream in; 
try { 
    in = getContentResolver().openInputStream(uri); 
    ExifInterface exifInterface = new ExifInterface(in); 
    // Now you can extract any Exif tag you want 
    // Assuming the image is a JPEG or supported raw format 
} catch (IOException e) { 
    // Handle any errors 
} finally { 
    if (in != null) { 
    try { 
     in.close(); 
    } catch (IOException ignored) {} 
    } 
} 

欲瞭解更多信息,檢查: Introducing the ExifInterface Support LibraryExifInterface

相關問題