2017-03-17 59 views
1

我正在尝试创建一个Android应用程序,但我无法通过点击按钮“共享音频.mp3”。这是我在java中的项目:分享音频按钮

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) { 

这应该是什么决议?提前致谢。

回答

0

在您的分享按钮的onClick(),添加以下代码:

Intent shareAudioIntent = new Intent(Intent.ACTION_SEND); 
shareAudioIntent.setType("audio/mp3"); 
shareAudioIntent.putExtra(Intent.EXTRA_STREAM, 
        Uri.parse("file://"+"path_to_your_mp3_file")); 
startActivity(Intent.createChooser(shareAudioIntent, "Share MP3 with:")); 

只需更换 “path_to_your_mp3_file” 与音频的路径。

如果您想共享资源文件夹中的文件,则必须先将其复制到存储中,然后从存储位置共享该文件。

快乐的编码。 !

+0

非常感谢。我会尝试它 –

+0

它完美的工作,但还有一个问题:当我尝试共享音频文件,并选择我想分享的应用程序时,有这样的消息:文件格式不支持。但它是一个普通的.mp3文件。我能做什么? –

+0

你正在分享mp3文件的应用程序是什么? – mohitum

0

首先从我的理解中,您试图通过单击按钮来共享音频文件。使用此代码片段来共享音频文件。只需在按钮点击功能中使用此功能即可。

String sharePath = Environment.getExternalStorageDirectory().getPath() 
      + "your mp3 path"; 
    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

你在哪里写过“你的路径mp3”,我应该写什么?因为如果我编写mp3文件的路径(R.raw.buonaseeeraaudio),当我使用应用程序中的按钮时,它会显示消息:不支持文件格式。非常感谢 –