2016-09-16 6 views
6

ファイル選択のコールバックIntentUriを返すため、これを尋ねるのは理由です。ExifデータをファイルではなくInputStreamから取得するには?

意向を経由してファイルを開くチュー:

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? 
    } 
} 

一つの方法は、実際のFileInputStreamを書くことになりますが、これは私にとって悪いの回避策のように思えます。

答えて

8

25.1.0サポートライブラリが導入された後、URIコンテンツ(content://またはfile://)からexifデータをInputStreamで読み取ることが可能になりました。

例:

コンパイル '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) {} 
    } 
} 

: まずあなたのGradleファイルに次の行を追加します。 Introducing the ExifInterface Support Library,ExifInterface

関連する問題