You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome Intent向安卓应用共享的数据有哪些?如何获取URL外的额外信息?

Chrome Intent Share: What Data Is Passed & How to Get Extra Metadata (Title, Images, Description)

Great question! Let’s break this down clearly—since Chrome doesn’t hand over those extra details by default, we’ll cover what you get out of the box and how to fetch the metadata you need.

First: What Data Does Chrome Actually Send via Intent?

When sharing a webpage from Chrome to your app using the standard ACTION_SEND intent, here’s the default payload you can rely on:

  • The webpage’s URL, typically delivered in Intent.EXTRA_TEXT (rarely in Intent.EXTRA_STREAM for web URLs)
  • In some edge cases, a short plain text snippet—but this is inconsistent and usually just the URL itself or a truncated string

Critical note: Chrome does not automatically extract and send metadata like page titles, Open Graph (OG) images, or descriptions via the intent. Those details aren’t part of the default share payload—Chrome only shares the core URL and minimal text.

How to Get the Extra Metadata (Title, Images, Description)

Since Chrome doesn’t pass these details directly, you’ll need to fetch and parse the webpage yourself once you have the URL. Here’s a step-by-step implementation:

1. Confirm You’re Receiving the URL Correctly

First, make sure your app’s intent filter is set up to handle shared web content, and that you’re extracting the URL properly. Example code for receiving the intent (Kotlin):

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val incomingIntent = intent
    val action = incomingIntent.action
    val type = incomingIntent.type

    if (Intent.ACTION_SEND == action && type != null && type.startsWith("text/")) {
        val sharedUrl = incomingIntent.getStringExtra(Intent.EXTRA_TEXT)
        sharedUrl?.let { url ->
            // Now we have the URL—time to fetch metadata
            fetchWebMetadata(url)
        }
    }
}

2. Fetch & Parse the Webpage Metadata

To extract title, images, and descriptions, you’ll need to:

  • Send a network request to the URL to get the raw HTML
  • Parse the HTML to find relevant meta tags (OG tags are the most reliable for shareable content; fall back to standard tags if needed)

We’ll use Jsoup—a lightweight, easy-to-use HTML parsing library for Android—to simplify this process.

Step 2a: Add Jsoup Dependency

Add this to your app-level build.gradle (Module level):

dependencies {
    implementation 'org.jsoup:jsoup:1.17.2'
}

Step 2b: Implement Metadata Fetching

Always run network operations on a background thread (we’ll use Kotlin Coroutines here—never run network calls on the main thread!). Example code:

private fun fetchWebMetadata(url: String) {
    CoroutineScope(Dispatchers.IO).launch {
        try {
            val doc = Jsoup.connect(url).get()
            
            // Prioritize Open Graph metadata (designed for sharing)
            val pageTitle = doc.select("meta[property=og:title]").attr("content")
                .takeIf { it.isNotEmpty() } ?: doc.title() // Fallback to page's <title> tag
            
            val pageDescription = doc.select("meta[property=og:description]").attr("content")
                .takeIf { it.isNotEmpty() } ?: doc.select("meta[name=description]").attr("content")
            
            val pageImageUrl = doc.select("meta[property=og:image]").first()?.absUrl("content")
            // Use absUrl() to convert relative image paths to absolute URLs
            
            // Move back to main thread to store data or update UI
            withContext(Dispatchers.Main) {
                saveMetadata(pageTitle, pageDescription, pageImageUrl, url)
            }
        } catch (e: IOException) {
            // Handle network errors (no internet, invalid URL, etc.)
            e.printStackTrace()
            withContext(Dispatchers.Main) {
                // Show an error message to the user if needed
            }
        }
    }
}

private fun saveMetadata(title: String, description: String, imageUrl: String?, originalUrl: String) {
    // Implement your storage logic here (e.g., Room database, SharedPreferences)
}

3. Required Permissions

Don’t forget to add the internet permission to your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

Key Considerations

  • Performance: Fetching and parsing a webpage takes time—show a loading indicator to keep users informed.
  • Fallback Logic: Not all sites use OG tags, so always have backups (like the page’s default <title> or description meta tag).
  • Image Handling: Some og:image URLs are relative—Jsoup’s absUrl() method fixes this by converting them to full URLs.
  • Security: Jsoup handles HTTPS by default, but be prepared to handle SSL certificate issues for niche sites.

内容的提问来源于stack exchange,提问作者user2682025

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:00:15