Skip to content Skip to sidebar Skip to footer

Sharing An Audio File

I am trying to share an audio file with a button, but when I click on it, the application shows a message 'The file format isn't supported'. How can I solve this? Here is my code B

Solution 1:

Try this Example:

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();     
            }
       });
}

Post a Comment for "Sharing An Audio File"