iT邦幫忙

2021 iThome 鐵人賽

DAY 2
0
Mobile Development

重新瞭解Android硬體控制系列 第 2

110/02 - 只有 StartActivityForResult 可以用嗎? - 1

前一天講到合約(Contracts)和啟動器(Launcher)取代StartActivityForResult,官方也幫我們建立了14種常見的合約模板,以下是官方的14種合約

ActivityResultContracts.CreateDocument()
ActivityResultContracts.GetContent()
ActivityResultContracts.GetMultipleContents()
ActivityResultContracts.OpenDocument()
ActivityResultContracts.OpenDocumentTree()
ActivityResultContracts.OpenMultipleDocuments()
ActivityResultContracts.PickContact()
ActivityResultContracts.RequestMultiplePermissions()
ActivityResultContracts.RequestPermission()
ActivityResultContracts.StartActivityForResult()
ActivityResultContracts.StartIntentSenderForResult()
ActivityResultContracts.TakePicture()
ActivityResultContracts.TakePicturePreview()
ActivityResultContracts.TakeVideo()

這篇先介紹這三種

ActivityResultContracts.CreateDocument()
ActivityResultContracts.GetContent()
ActivityResultContracts.GetMultipleContents()

ActivityResultContracts.CreateDocument()

An ActivityResultContract to prompt the user to select a path for creating a new document, returning the content: Uri of the item that was created.
The input is the suggested name for the new file.

This can be extended to override createIntent if you wish to pass additional extras to the Intent created by super.createIntent().

CreateDocument()可以用在拍照前的建立空白檔案。

以下範例是建立空白的saberEat.jpg檔案,然後回傳檔案的content://uri,需要注意使用者可以更改檔案名稱。

createDocumentResultLauncher.launch("saberEat.jpg")
private val createDocumentResultLauncher =
    registerForActivityResult(ActivityResultContracts.CreateDocument()) { uri ->
        Log.d("maho", "回傳: $uri")
    }

實際執行程式後的Log

D/maho: 回傳: content://com.android.providers.downloads.documents/document/1692

ActivityResultContracts.GetContent()

An ActivityResultContract to prompt the user to pick a piece of content, receiving a content:// Uri for that content that allows you to use android.content.ContentResolver.openInputStream(Uri) to access the raw data. By default, this adds Intent.CATEGORY_OPENABLE to only return content that can be represented as a stream.

The input is the mime type to filter by, e.g. image/*.
This can be extended to override createIntent if you wish to pass additional extras to the Intent created by super.createIntent().

以下範例是輸入一個MIME type指定類型,然後選擇一個檔案,回傳檔案的content://uri,不可以輸入null

getContentResultLauncher.launch("image/*")
private val getContentResultLauncher =
    registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
        Log.d("maho", "回傳: $uri")
    }

實際執行程式後的Log

D/maho: 回傳: content://com.android.providers.downloads.documents/document/1688

ActivityResultContracts.GetMultipleContents()

An ActivityResultContract to prompt the user to pick one or more a pieces of content, receiving a content:// Uri for each piece of content that allows you to use android.content.ContentResolver.openInputStream(Uri) to access the raw data. By default, this adds Intent.CATEGORY_OPENABLE to only return content that can be represented as a stream.

The input is the mime type to filter by, e.g. image/*.

This can be extended to override createIntent if you wish to pass additional extras to the Intent created by super.createIntent().

以下範例是輸入一個MIME type指定類型,然後長按選擇多個檔案,用陣列的形式回傳檔案的content://uri,不可以輸入null

getMultipleContentsResultLauncher.launch("image/*")
private val getMultipleContentsResultLauncher =
    registerForActivityResult(ActivityResultContracts.GetMultipleContents()) { uri ->
        Log.d("maho", "回傳: $uri")
    }

實際執行程式後的Log

D/maho: 回傳: [
content://com.android.providers.downloads.documents/document/1688,
content://com.android.providers.downloads.documents/document/1677
]

程式碼放在feature/resultTemplate分支
https://github.com/AndyAWD/AndroidSystem/tree/feature/resultTemplate


上一篇
110/01 - 什麼!startActivityForResult 被標記棄用?
下一篇
110/03 - 只有 StartActivityForResult 可以用嗎? - 2
系列文
重新瞭解Android硬體控制19

尚未有邦友留言

立即登入留言