2017-04-02 80 views
0

我想用按鈕分享音頻文件,但是當我點擊它時,應用程序會顯示一條消息「不支持文件格式」。我該如何解決這個問題?這裏是我的代碼分享音頻文件

Button buonaseeera=(Button) findViewById(R.id.pulsantebuonaseeera); 



    buonaseeera.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      audiobuonaseeera=MediaPlayer.create(getApplicationContext(), R.raw.buonaseeeraaudio); 
      audiobuonaseeera.start(); 





    Button sharebutton=(Button) findViewById(R.id.sharebutton); 
      sharebutton.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        String sharePath = 
Environment.getExternalStorageDirectory().getPath() 
          + "raw2sd"; 
        Uri uri = Uri.parse(sharePath); 
        Intent share = new Intent(Intent.ACTION_SEND); 
        share.setType("audio/*"); 
        share.putExtra(Intent.EXTRA_STREAM, uri); 
        startActivity(Intent.createChooser(share, "Share Audio 
File")); 
+0

那麼也許it's不支持的音頻...哪個是真的? – Opiatefuchs

+0

使用正確的MIME類型,而不是通配符。 – CommonsWare

+0

這是一個.mp3 audiio文件 –

回答

0

嘗試這個例子:

public class MusicPlayerActivity extends AppCompatActivity { 
Activity mactivity; 
private Button btn_shareaudio; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_music_player); 

     mactivity = this; 

     btn_shareaudio = (Button) findViewById(R.id.btn_activity_music_player_shareaudio); 

     btn_shareaudio.setOnClickListener(
      new View.OnClickListener() { 
      @Override 
      public void onClick(View v) {  
      File f = new File(/*Path of the song*/); 
      Uri uri = Uri.parse("file://" + f.getAbsolutePath()); 
      Intent share = new Intent(Intent.ACTION_SEND); 
      share.putExtra(Intent.EXTRA_STREAM, uri); 
      share.setType("audio/*"); 
      share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
      mactivity.startActivity(Intent.createChooser(share, "Share audio File")); 

      Toast.makeText(getApplicationContext(), "Song Shared Successfully", Toast.LENGTH_SHORT).show();  
      } 
     }); 
}