接收多媒體內容

圖 1.無論特定使用者介面機制為何,統合式 API 都能在單一位置處理傳入的內容,例如透過按住選單貼上或使用拖曳功能。

使用者喜歡圖片、影片和其他生動的內容,但在應用程式中插入及移動這類內容並不容易。為簡化應用程式接收多媒體內容,Android 12 (API 級別 31) 推出了整合式 API,可讓應用程式接受任何來源的內容,包括剪貼簿、鍵盤或拖曳。

您可以將 OnReceiveContentListener 等介面附加至使用者介面元件,並在透過任何機制插入內容時接收回呼。回呼會成為程式碼的單一位置,處理接收從純文字和樣式化文字到標記、圖片、影片、音訊檔案等所有內容。

為了提供與 Android 舊版本的回溯相容性,從 Core 1.7Appcompat 1.4 起,AndroidX 也提供這個 API,建議您在實作這項功能時使用。

總覽

在其他現有的 API 中,每個 UI 機制 (例如按住選單或拖曳) 都有專屬的對應 API。這表示您必須分別整合每個 API,針對插入內容的每個機制新增類似的程式碼:

這張圖片顯示各種動作與要導入的相對 API
圖 2 先前,應用程式為每個插入內容的 UI 機制實作不同的 API。

OnReceiveContentListener API 會建立用於導入的單一 API 來整合這些不同程式碼的路徑,以便您可以專心處理應用程式的專屬邏輯,讓平台處理其餘的工作:

顯示簡化的整合式 API 的圖片
圖 3. 統合式 API 讓您可以導入支援所有 UI 機制的單一 API。

此外,這個做法也表示在平台中新增插入內容的方法時,您不需要進行其他程式碼變更,即可在應用程式中啟用支援功能。如果應用程式需要針對特定用途導入完整的自訂功能,您仍可使用現有的 API,此 API 仍會以相同方式繼續運作。

導入作業

API 是具有單一方法 OnReceiveContentListener 的事件監聽器介面。如要支援較舊版本的 Android 平台,建議您使用 AndroidX Core 程式庫中比對符合的 OnReceiveContentListener 介面。

如要使用 API,請指定應用程式可以處理的內容類型,來實作事件監聽器:

Kotlin

object MyReceiver : OnReceiveContentListener {
    val MIME_TYPES = arrayOf("image/*", "video/*")
    
    // ...
    
    override fun onReceiveContent(view: View, payload: ContentInfoCompat): ContentInfoCompat? {
        TODO("Not yet implemented")
    }
}

Java

public class MyReceiver implements OnReceiveContentListener {
     public static final String[] MIME_TYPES = new String[] {"image/*", "video/*"};
     // ...
}

指定應用程式支援的所有內容 MIME 類型後,再導入事件監聽器的其餘部分:

Kotlin

class MyReceiver : OnReceiveContentListener {
    override fun onReceiveContent(view: View, contentInfo: ContentInfoCompat): ContentInfoCompat {
        val split = contentInfo.partition { item: ClipData.Item -> item.uri != null }
        val uriContent = split.first
        val remaining = split.second
        if (uriContent != null) {
            // App-specific logic to handle the URI(s) in uriContent.
        }
        // Return anything that your app didn't handle. This preserves the
        // default platform behavior for text and anything else that you aren't
        // implementing custom handling for.
        return remaining
    }

    companion object {
        val MIME_TYPES = arrayOf("image/*", "video/*")
    }
}

Java

 public class MyReceiver implements OnReceiveContentListener {
     public static final String[] MIME_TYPES = new String[] {"image/*", "video/*"};

     @Override
     public ContentInfoCompat onReceiveContent(View view, ContentInfoCompat contentInfo) {
         Pair split = contentInfo.partition(
                 item -> item.getUri() != null);
         ContentInfo uriContent = split.first;
         ContentInfo remaining = split.second;
         if (uriContent != null) {
             // App-specific logic to handle the URI(s) in uriContent.
         }
         // Return anything that your app didn't handle. This preserves the
         // default platform behavior for text and anything else that you aren't
         // implementing custom handling for.
         return remaining;
     }
 }

如果您的應用程式已支援與意圖共用資料,您可以重複使用應用程式指定的邏輯來處理內容 URI。傳回任何其餘資料,委派給平台進行資料處理。

導入事件監聽器後,請將其設定在應用程式中的適當 UI 元素上:

Kotlin

class MyActivity : Activity() {
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // ...
        val myInput = findViewById(R.id.my_input)
        ViewCompat.setOnReceiveContentListener(myInput, MyReceiver.MIME_TYPES, MyReceiver())
    }
}

Java

public class MyActivity extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState) {
         // ...

         AppCompatEditText myInput = findViewById(R.id.my_input);
         ViewCompat.setOnReceiveContentListener(myInput, MyReceiver.MIME_TYPES, new MyReceiver());
     }
}

URI 權限

在傳遞至 OnReceiveContentListener 的酬載中,平台會自動授予和發布任何內容 URI 的讀取權限。

一般來說,應用程式會在服務或活動中處理內容 URI。對於長時間執行的處理作業,請使用 WorkManager。實作這項權限時,請使用 Intent.setClipData設定旗標 FLAG_GRANT_READ_URI_PERMISSION,將權限擴充至目標服務或活動。

或者,您也可以透過目前的結構定義,使用背景執行緒來處理內容。在這種情況下,您必須維護事件監聽器收到的 payload 物件參照,以確保平台不會提前撤銷權限。

自訂檢視畫面

如果您的應用程式使用自訂 View 子類別,請務必確保系統不會略過 OnReceiveContentListener

如果 View 類別覆寫 onCreateInputConnection 方法,請使用 Jetpack API InputConnectionCompat.createWrapper 設定 InputConnection

如果 View 類別會覆寫 onTextContextMenuItem 方法,請在選單項目為 R.id.pasteR.id.pasteAsPlainText 時委派給超級類別。

與鍵盤圖片 API 比較

您可以將 OnReceiveContentListener API 想成是現有 鍵盤映像檔 API 的下一個版本。這個統合式 API 支援鍵盤映像檔 API 的功能,以及一些其他功能。視您使用 Jetpack 程式庫或 Android SDK 中的原生 API 而定,裝置和功能相容性會有所不同。

表 1. Jetpack 支援的功能和 API 級別。
動作或功能 鍵盤映像檔 API 支援 統合式 API 支援
透過鍵盤插入 是 (API 級別 13 以上) 是 (API 級別 13 以上)
透過按住選單的貼上功能插入
使用拖曳功能插入 是 (API 級別 24 以上)
表 2. 原生 API 支援的功能和 API 級別。
動作或功能 鍵盤映像檔 API 支援 統合式 API 支援
透過鍵盤插入 是 (API 級別 25 以上) 是 (Android 12 以上版本)
透過按住選單的貼上功能插入
使用拖曳功能插入