2011-12-26 39 views
4

我試着通過WhatsApp的共享MP3文件。它可以與其他應用程序(如Gmail)完美協作,但它可以在whatsapp上運行。誰能幫我?我需要添加一些putExtra()嗎?Intent.ACTION_SEND WhatsApp的

這裏是我的代碼:

public void shareWithFriends(int id) 
{  
    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("audio/mp3"); 
    //share.putExtra(Intent.EXTRA_SUBJECT,"subject"); 
    //Uri uri = Uri.parse("android.resource://com.igs.pokemonsoundboard/" + id); 
    Uri uri = Uri.parse("android.resource://com.igs.pokemonsoundboard/raw/" + R.raw.pikachump3); 
    share.putExtra(Intent.EXTRA_STREAM,uri); 
    //share.putExtra("sms_body","Ringtone File :"); 
    startActivity(Intent.createChooser(share, "Share sound")); 
} 

感謝;)

回答

6

您應該複製您的音頻文件到SD卡,並分享它的文件,而不是Android的資源,像這樣:

final Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); 
shareIntent.setType("audio/mp3"); 
shareIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://"+path+filename)); 
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_sound))); 

現在它應該通過whatsapp。

+1

此代碼不適用於我,即使我已將該文件保存在「sdcard」中,但我收到'無法發送,請重試'消息 – 2014-12-17 11:19:20

0

嘗試改變MIME類型 「音頻/ MPEG3」,這樣第二行顯示

share.setType("audio/mpeg3") 
+0

感謝onielse14,但它dosent工程。我嘗試將類型更改爲mpeg3,但沒有成功。任何其他想法? – user1116665 2011-12-26 19:39:33

0

的WhatsApp也接受OGG格式:

Intent shareIntent = new Intent(Intent.ACTION_SEND); 
shareIntent.setType("audio/ogg"); 
shareIntent.putExtra(Intent.EXTRA_STREAM, getSoundUri()); 
startActivity(shareIntent); 
1

你必須在你的代碼包括此:

sendIntent.setPackage("com.whatsapp");