2016-09-03 4 views
0

私はこのコードスニペットが見つかると、アプリを作るためにアンドロイドスタジオの知識を得ようとしています。いつ、アンドロイドスタジオでFileProviderを使用するのですか?完全に初心者のプログラマー

enter code herestatic final int REQUEST_TAKE_PHOTO = 1; 
private void dispatchTakePictureIntent() { 
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
// Ensure that there's a camera activity to handle the intent 
if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
    // Create the File where the photo should go 
    File photoFile = null; 
    try { 
     photoFile = createImageFile(); 
    } catch (IOException ex) { 
     // Error occurred while creating the File 
     ... 
    } 
    // Continue only if the File was successfully created 
    if (photoFile != null) { 
     Uri photoURI = FileProvider.getUriForFile(this, 
               "com.example.android.fileprovider", 
               photoFile); 
     takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
     startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
    } 
} 

}

私はほとんどすべてのコードを理解するが、私はちょうど "FileProvider" と混同!! なんですか?私たちがそれを使う時?あなたのコードの場合は

+0

[FileProvider](https://developer.android.com/reference/android/support/v4/ content/FileProvider.html) –

答えて

0

、それはそのURI(場所を取得するためにハンドルを使用して(写真の)実際のファイルを作成するために使用されるとhandle ...

// Create the File where the photo should go 
    File photoFile = null; 
    try { 
     photoFile = createImageFile(); 
    } catch (IOException ex) { 
     // Error occurred while creating the File 
     ... 
    } 

..thenを得ていました

Uri photoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider", photoFile); 

...写真を撮っています。 (URI)、その場所に保存

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 

ほとんどあなたが提供されているコードではよくコメントし

+0

このすべての回答に感謝します。私は本当に新しい情報を得る。 _Uri_が何のために使われているのか教えてください。 –

+0

URIは[Uniform Resource Identifier](https://tools.ietf.org/html/rfc3986)です。これは、抽象的または物理的なリソースを識別するコンパクトな文字列です。リソースの名前を付けるために使用できます(上で使用したように)。これは、特定のアイテムへのファイルパスまたはウェブまたはftpアドレスに似ています。この場合、撮影された写真を保存するファイルの場所が示されます。それ以外の場合(Googleの残りの部分)[https://www.google.com/search?q=define%3Auri] ...(Androidデベロッパー)[https://developer.android.com]/index .html – user919426

関連する問題