如何创建类似Tez的分享Intent?实现带图标与超链接文本的应用分享
Hey there! Let's break down how to build that Tez-style share intent for your Android app. I've implemented this exact flow a few times, so here's a reliable, step-by-step guide:
核心思路
Tez's share effect works by combining rich hyperlinked text and your app's icon as an attached media asset. This lets receiving apps display both elements together, just like Tez does.
Step 1: Build Hyperlinked Rich Text
First, create clickable hyperlinked text. Android supports HTML-formatted text for this, so we'll use Html.fromHtml() to convert an HTML string into a shareable Spanned object.
// Kotlin example val shareHtmlText = Html.fromHtml( "Check out my awesome app! <a href=\"https://your-app-url.com\">Download now</a>", Html.FROM_HTML_MODE_LEGACY )
Note: For Android N (API 24+) and above, use
FROM_HTML_MODE_LEGACYor other appropriate modes. If sharing an app deep link instead of a web URL, replace the href value with your deep link scheme (e.g.,app://yourapp/open-feature).
Step 2: Prepare Your App Icon as a Shareable Uri
You can't directly pass your app's drawable for sharing—Android requires a Content Uri (via FileProvider) so other apps can safely access the image. Here's how to set this up:
2.1 Register FileProvider in AndroidManifest.xml
Add this inside your <application> tag:
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
2.2 Create the File Paths XML
Make a new file res/xml/file_paths.xml to define where shared files are stored:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="app_icons" path="icons/" /> </paths>
2.3 Convert App Icon to Content Uri
Write a helper function to save your app's drawable to a file and generate the Uri:
private fun getAppIconContentUri(context: Context): Uri? { return try { // Get your app's icon drawable val appIcon = context.packageManager.getApplicationIcon(context.packageName) // Create a directory to save the icon in internal storage val iconDir = File(context.filesDir, "icons") iconDir.mkdirs() val iconFile = File(iconDir, "app_icon.png") // Save the drawable as a PNG file val outputStream = FileOutputStream(iconFile) (appIcon as BitmapDrawable).bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream) outputStream.close() // Generate the Content Uri using FileProvider FileProvider.getUriForFile( context, "${context.packageName}.fileprovider", iconFile ) } catch (e: Exception) { // Handle errors gracefully (fall back to text-only share if needed) e.printStackTrace() null } }
Step 3: Assemble the Share Intent
Combine the hyperlinked text and icon Uri into a single share intent. We use ACTION_SEND_MULTIPLE since we're sharing two types of content (text + image):
fun launchTezStyleShare(context: Context) { val hyperlinkedText = Html.fromHtml( "Try my new app! <a href=\"https://your-app-url.com\">Get it here</a>", Html.FROM_HTML_MODE_LEGACY ) val iconUri = getAppIconContentUri(context) val shareIntent = if (iconUri != null) { // Share both text and icon Intent(Intent.ACTION_SEND_MULTIPLE).apply { type = "*/*" // Supports both text and image formats putExtra(Intent.EXTRA_TEXT, hyperlinkedText) putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayListOf(iconUri)) putExtra(Intent.EXTRA_SUBJECT, "Check out this app!") // Grant read permission to the receiving app addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) } } else { // Fallback to text-only share if icon generation fails Intent(Intent.ACTION_SEND).apply { type = "text/html" putExtra(Intent.EXTRA_TEXT, hyperlinkedText) } } // Show the system share chooser context.startActivity(Intent.createChooser(shareIntent, "Share via")) }
Key Compatibility Notes
- Receiver App Support: Not all apps will display both the text and icon—some might only show one or the other. This depends on how the receiving app handles shared content, so always plan for fallback behavior.
- Deep Links: If sharing an app deep link, make sure your app has the corresponding intent filters in the manifest to handle the link scheme.
- Error Handling: Never skip error handling for icon generation—falling back to text-only share ensures your feature works even if something goes wrong with the icon.
内容的提问来源于stack exchange,提问作者Manoj Perumarath

