Share Audio File in Android – Share Intent Example 2017

how to share audio file in android programmatically

Share audio file in android both from mobile internal memory and from your application raw folder. In some cases we required to send our audio file to other user using intent here is the complete code. Share audio fie to Facebook, Mesenger, Whatsapp, Email or any other sharing intent which support audio file sharing. Android single button to play and pause music.

In this first example get code of share audio file from Mobile own internal or SD card memory.

1. Android share audio file from internal storage

Button audioShare = (Button) findViewById(R.id.shareAu);
audioShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//Keep test.pcm or any other format audio file in your Memory card. Otherwise it will not work.
String sharePath = Environment.getExternalStorageDirectory().getPath() + “test.pcm”;
Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType(“audio/*”);   //Any audio file type you can share.
//share.setType(“audio/mp3”);   //For particular audio file type.
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, “Share Sound File”));
}
});

Add this code to your activity page.

2. Android share mp3 file from res/raw folder

If you want to share audio file from your app you can see in this code there is some similarity with the above code. Audio file will be shared using file share intent and if from raw folder file is copied to internal then file is ready to share. You can also learn how to save image as wallpaper in android in sharedPrefernces.

Button rawA = (Button) findViewById(R.id.rawAudio);
rawA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

InputStream inputStream;
FileOutputStream fileOutputStream;
try {
inputStream = getResources().openRawResource(R.raw.sound);
fileOutputStream = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), “sound.mp3”));

byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}

inputStream.close();
fileOutputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(“file://” + Environment.getExternalStorageDirectory() + “/sound.mp3” ));
intent.setType(“audio/*”);
startActivity(Intent.createChooser(intent, “Share sound”));
}
});

AndroidManifest.xml source code for audio file share

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”utube.attraction.addnewplus”>

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />
<uses-permission android:name=”android.permission.RECORD_AUDIO” />

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.SaveFile” />
<activity android:name=”.CropImage” />
<activity android:name=”.TintImage” />
<activity android:name=”.AddWallPaper” />
<activity android:name=”.ShareFile” />
<activity android:name=”.SoundLevel”></activity>
</application>

</manifest>

Add a Comment

Your email address will not be published. Required fields are marked *