1

I'm developing (using Xamarin) an Android application to play and share preloaded audio files.

I have some mp3 files in the raw folder (inside the Resources' one) that I can play in the media player doing this:

protected override void OnStart()
{
    base.OnStart();
    gBtnName1 = FindViewById<Button>(Resource.Id.btn1);
    gBtnName1.Click += GBtnName1_Click;
    gBtnName1.LongClick += GBtnName1_LongClick;
}

When the user clicks BtnName1 (for example) the following method plays the mp3 file in the device's media player.

private void GBtnName1_Click(object sender, EventArgs e)
{
    player = MediaPlayer.Create(this, Resource.Raw.Name1);
    player.Start();
}

That's working just fine! :)

The idea is to give the user the possibility of sharing the audio (via Email, WhatsApp, etc.) when the button is long-pressed, but I have not yet managed to make these mp3 files accessible from outside the application :'(

Can anyone help me? I know I've to copy the mp3 file from the raw folder to the phone's memory, but I don't know how to do it!

private void GBtnName1_LongClick(object sender, View.LongClickEventArgs e)
{
    //Get the mp3 file from resources and save it to the external storage.
    //Once I have the "public Uri" use Intent.CreateChooser
    //to share this file via Email, WhatsApp, Bluetooth, etc.
    var sharingIntent = new Intent();
    sharingIntent.SetAction(Intent.ActionSend);
    sharingIntent.SetType("audio/mp3");
    sharingIntent.PutExtra(Intent.ExtraStream, publicUri);
    sharingIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
    StartActivity(Intent.CreateChooser(sharingIntent, "Share using..."));
}

Thanks in advance!

  • As I understand, your problem is that you can't share a file which is bundled with the .apk? If so, and you want to move the file to, say, external storage, you can [try this](http://stackoverflow.com/questions/8664468/copying-raw-file-into-sdcard). Then you simply need to use the new path which you have moved the file to. – Demitrian Jan 14 '17 at 12:50
  • Possible duplicate of [Copying raw file into SDCard?](http://stackoverflow.com/questions/8664468/copying-raw-file-into-sdcard) – Demitrian Jan 14 '17 at 12:50

0 Answers0