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

如何将WhatsApp音频分享至Android应用?求相关技术资源

Hey there! I’ve tackled this exact reverse sharing problem before when building an Android media app, so let me walk you through the steps to get WhatsApp audio into your app:

1. Register Your App to Receive Shared Audio via Intent Filters

Android’s intent system lets apps declare they can accept shared content. You’ll need to add intent filters in your AndroidManifest.xml to tell the system your app can handle audio shared from WhatsApp.

Add this inside the activity tag that will receive the audio:

<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="audio/*" />
</intent-filter>

2. Handle the Incoming Audio in Your Activity

Once your app shows up in WhatsApp’s share sheet, you need to process the incoming audio URI. Add this code to your activity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    
    // Check if the activity was launched via a share intent
    intent?.let { handleSharedAudio(it) }
}

// For when the app is already running and gets a new share intent
override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    intent?.let { handleSharedAudio(it) }
}

private fun handleSharedAudio(intent: Intent) {
    if (Intent.ACTION_SEND == intent.action && intent.type?.startsWith("audio/") == true) {
        val audioUri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
        audioUri?.let { uri ->
            // Use ContentResolver to access the audio file
            contentResolver.openInputStream(uri)?.use { inputStream ->
                // Copy the audio to your app's internal storage or process it directly
                // Example: write to a file
                val outputFile = File(filesDir, "shared_audio.mp3")
                FileOutputStream(outputFile).use { outputStream ->
                    inputStream.copyTo(outputStream)
                }
            }
        }
    }
}

Important note: WhatsApp shares audio as content:// URIs, so you must use ContentResolver to access the file—you can’t directly read the file path from the URI.

3. Support Multiple Audio Files (Optional)

If you want to accept multiple audio files at once, add an extra intent filter:

<intent-filter>
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="audio/*" />
</intent-filter>

Then update your handling code to process a list of URIs:

private fun handleSharedAudio(intent: Intent) {
    when (intent.action) {
        Intent.ACTION_SEND -> {
            // Handle single audio file as above
        }
        Intent.ACTION_SEND_MULTIPLE -> {
            val audioUris = intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)
            audioUris?.forEach { uri ->
                // Process each audio URI individually
            }
        }
    }
}

4. Permission Checks

  • For Android 10 (API 29+) and above: No extra permissions are needed—Android grants temporary access to shared content via the URI.
  • For older Android versions: Declare the READ_EXTERNAL_STORAGE permission in your manifest, and request runtime permission if targeting API 23+:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

5. Test the Flow

To make sure it works:

  • Open WhatsApp, select an audio message or saved audio file
  • Tap the share button, your app should appear in the share sheet
  • Select your app, and it should receive and process the audio correctly

If your app doesn’t show up in the share sheet, double-check your intent filters—make sure the mime type is set to audio/* and the actions are declared properly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 21:37:33