وقتی برنامهای میخواهد به فایلی که برنامه دیگری همرسانی کرده است دسترسی پیدا کند، برنامه درخواستکننده (کارخواه)
معمولاً درخواستی به برنامه همرسانیکننده فایلها (سرور) ارسال میکند. در بیشتر موارد، درخواست
Activity را در برنامه سرور شروع میکند که فایلهایی را که میتواند همرسانی کند نمایش میدهد.
کاربر فایلی را انتخاب میکند، سپس برنامه سرور شناسه منبع یکسان محتوای فایل را به برنامه کارخواه برمیگرداند.
این درس به شما نشان میدهد که چگونه یک برنامه مشتری فایلی را از یک برنامه سرور درخواست میکند، شناسه منبع یکسان محتوای فایل را از برنامه سرور دریافت میکند، و فایل را بااستفاده از شناسه منبع یکسان محتوا باز میکند.
ارسال درخواست برای فایل
برای درخواست فایل از برنامه سرور، برنامه کارخواه
startActivityForResult را با
Intent حاوی کنشی مانند
ACTION_PICK و نوع MIME که برنامه کارخواه
میتواند مدیریت کند فراخوانی میکند.
برای مثال، تکهکد زیر نشان میدهد که چگونه میتوان یک
Intent را به برنامه سرور ارسال کرد تا
Activity شرحدادهشده در همرسانی فایل را شروع کرد:
کاتلین
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) ... } ... }
جاوا
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); ... } ... }
دسترسی به فایل درخواستشده
برنامه سرور شناسه منبع یکسان محتوای فایل را در
Intent به برنامه کارخواه برمیگرداند. این Intent به برنامه کارخواه
در لغو onActivityResult() ارسال میشود. وقتی
برنامه کارخواه شناسه منبع یکسان محتوای فایل را داشته باشد، میتواند با دریافت
FileDescriptor به فایل دسترسی پیدا کند.
امنیت فایل در این فرایند فقط تا زمانی حفظ میشود که شناسه منبع یکسان محتوا را که برنامه کارخواه دریافت میکند بهدرستی تجزیه کنید. هنگام تجزیه محتوا، باید مطمئن شوید که این URI به چیزی خارج از فهرست راهنمای موردنظر اشاره نمیکند، و مطمئن شوید که هیچ پیمایش مسیری انجام نمیشود. فقط برنامه کارخواه باید به فایل دسترسی پیدا کند و فقط برای اجازههایی که برنامه سرور اعطا کرده است. اجازهها موقتی هستند، بنابراین وقتی پشته تکلیف برنامه کارخواه تمام شد، فایل دیگر خارج از برنامه سرور قابلدسترسی نیست.
تکهکد بعدی نشان میدهد که برنامه کلاینت چگونه
Intent ارسالشده از برنامه سرور را مدیریت میکند و چگونه برنامه کلاینت
FileDescriptor را بااستفاده از شناسه منبع یکسان محتوا دریافت میکند:
کاتلین
/* * 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 ... } }
جاوا
/* * 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 دریافت میکند که سپس میتواند از آن برای خواندن فایل استفاده کند.
برای اطلاعات مرتبط بیشتر، به این مقاله مراجعه کنید: