アプリが別のアプリで共有されているファイルにアクセスする場合、リクエスト元のアプリ(クライアント)は
通常は、ファイルを共有するアプリ(サーバー)にリクエストを送信します。ほとんどの場合、このリクエストは
は、共有可能なファイルを表示するサーバーアプリで Activity
を開始します。
ユーザーがファイルを選択すると、サーバー アプリはファイルのコンテンツ URI をクライアントに返します。
ダウンロードします。
このレッスンでは、クライアント アプリがサーバーアプリにファイルをリクエストし、そのファイルのトークンを サーバーアプリからコンテンツ URI を受け取り、そのコンテンツ URI を使用してファイルを開きます。
ファイルのリクエストを送信する
サーバーアプリからファイルをリクエストするには、クライアント アプリが
startActivityForResult
を
次のようなアクションを含む Intent
ACTION_PICK
と、クライアント アプリが返す MIME タイプ
対処できます。
たとえば、次のコード スニペットは、
サーバーアプリに Intent
を追加して、
Activity
については、ファイルの共有をご覧ください。
Kotlin
class MainActivity : Activity() { private lateinit var requestFileIntent: Intent private lateinit var inputPFD: ParcelFileDescriptor ... override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) requestFileIntent = Intent(Intent.ACTION_PICK).apply { type = "image/jpg" } ... } ... private fun requestFile() { /** * When the user requests a file, send an Intent to the * server app. * files. */ startActivityForResult(requestFileIntent, 0) ... } ... }
Java
public class MainActivity extends Activity { private Intent requestFileIntent; private ParcelFileDescriptor inputPFD; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); requestFileIntent = new Intent(Intent.ACTION_PICK); requestFileIntent.setType("image/jpg"); ... } ... protected void requestFile() { /** * When the user requests a file, send an Intent to the * server app. * files. */ startActivityForResult(requestFileIntent, 0); ... } ... }
リクエストされたファイルにアクセスする
サーバーアプリは、ファイルのコンテンツ URI を
Intent
。この Intent
がクライアントに渡されます。
onActivityResult()
のオーバーライドでオーバーライドできます。1 回
クライアント アプリはファイルのコンテンツ URI を持っている場合、そのファイルのコンテンツ URI を取得することでファイルにアクセスできます。
FileDescriptor
。
コンテンツ URI を適切に解析している場合にのみ、このプロセスでファイルのセキュリティが確保される クライアント アプリが受け取るもの。コンテンツを解析するときは、この URI が 意図したディレクトリの外にあるすべてのファイルに パス トラバーサルが試行されている。 クライアント アプリのみがファイルにアクセスできるようにし、クライアントによって付与された権限にのみアクセスします。 使用します。権限は一時的なものであるため、クライアント アプリのタスクスタックが終了すると、 サーバーアプリ外からはアクセスできなくなります。
次のスニペットは、クライアント アプリが
サーバーアプリから送信される Intent
と、クライアント アプリが
コンテンツ URI を使用する FileDescriptor
:
Kotlin
/* * When the Activity of the app that hosts files sets a result and calls * finish(), this method is invoked. The returned Intent contains the * content URI of a selected file. The result code indicates if the * selection worked or not. */ public override fun onActivityResult(requestCode: Int, resultCode: Int, returnIntent: Intent) { // If the selection didn't work if (resultCode != Activity.RESULT_OK) { // Exit without doing anything else return } // Get the file's content URI from the incoming Intent returnIntent.data?.also { returnUri -> /* * Try to open the file for "read" access using the * returned URI. If the file isn't found, write to the * error log and return. */ inputPFD = try { /* * Get the content resolver instance for this context, and use it * to get a ParcelFileDescriptor for the file. */ contentResolver.openFileDescriptor(returnUri, "r") } catch (e: FileNotFoundException) { e.printStackTrace() Log.e("MainActivity", "File not found.") return } // Get a regular file descriptor for the file val fd = inputPFD.fileDescriptor ... } }
Java
/* * When the Activity of the app that hosts files sets a result and calls * finish(), this method is invoked. The returned Intent contains the * content URI of a selected file. The result code indicates if the * selection worked or not. */ @Override public void onActivityResult(int requestCode, int resultCode, Intent returnIntent) { // If the selection didn't work if (resultCode != RESULT_OK) { // Exit without doing anything else return; } else { // Get the file's content URI from the incoming Intent Uri returnUri = returnIntent.getData(); /* * Try to open the file for "read" access using the * returned URI. If the file isn't found, write to the * error log and return. */ try { /* * Get the content resolver instance for this context, and use it * to get a ParcelFileDescriptor for the file. */ inputPFD = getContentResolver().openFileDescriptor(returnUri, "r"); } catch (FileNotFoundException e) { e.printStackTrace(); Log.e("MainActivity", "File not found."); return; } // Get a regular file descriptor for the file FileDescriptor fd = inputPFD.getFileDescriptor(); ... } }
メソッド openFileDescriptor()
ファイルの ParcelFileDescriptor
を返します。このオブジェクトから、クライアントは
アプリは FileDescriptor
オブジェクトを取得し、このオブジェクトを使用してファイルを読み取ることができます。
その他の関連情報については、以下をご覧ください。