2015-10-01 41 views
0

我想製作一個按鈕在電報上共享音頻文件。 我只有電報的問題(在WhatsApp的共享工作正常)。如何在電報上共享音頻

我的音頻文件在原始文件夾中,並且我嘗試使用.mp3,.wav和.m4a擴展名,但是如果我嘗試在電報上共享音頻,我會得到「不支持的附件」的祝詞。

這是我的一份方法:

Intent share = new Intent(Intent.ACTION_SEND); 
share.setType("audio/m4a"); 
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + ctx.getPackageName() + "/" + R.raw.my_audio)); 
startActivity(Intent.createChooser(share, "Share on:")); 

回答

0

許多Android應用程序將無法辦理虧待android.resource://方案。如果您希望獲得更好的兼容性,請將音頻寫入文件並共享,或使用(例如,my StreamProvider)通過content://方案提供。

+0

我該如何使用ContentProvider? – genialFactory

+0

@genialFactory:您必須編寫一個將資源內容從'openFile()'或可能''openAssetFile()'返回的管道。 [這個示例應用程序](https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/GrantUriPermissions)顯示了基本概念,儘管它在'assets /'中使用了一個文件而不是原始資源。 – CommonsWare

4

使用「Uri.fromFile」,這爲我工作。

   File file = new File(filePath); 
       Uri uri = Uri.fromFile(file); 
       Intent share = new Intent(Intent.ACTION_SEND); 
       share.setType("audio/*"); 
       share.putExtra(Intent.EXTRA_STREAM, uri); 
       startActivity(Intent.createChooser(share, 
         "به اشتراک گذاشتن فایل")); 
+0

重點是使用Uri.fromFile()而不是Uri.parse() –