Flyentology
Flyentology

Reputation: 121

Sending music file from one application to another

I'm trying to send a music files from my app to other ones using this code. I already have WRITE_EXTERNAL_STORAGE permission enabled. But whenever I choose the app I want to share my file with it doesn't appear or I get Toast saying format not recognized.

                                    String filePath = songs.get(viewPosition).getPath();
                                    Uri uri = Uri.parse(filePath);
                                    Intent share = new Intent(Intent.ACTION_SEND);
                                    share.setType("audio/*");
                                    share.putExtra(Intent.EXTRA_STREAM, uri);
                                    mContext.startActivity(Intent.createChooser(share, "Share Sound File"));

Path I'm getting from filePath is for example something like this: /storage/emulated/0/Music/2Pac - better dayz - tupac - better days.mp3

Upvotes: 1

Views: 634

Answers (2)

Mbuodile Obiosio
Mbuodile Obiosio

Reputation: 1543

From the answer from @Flyentology I had to make a little change to get it to work.

<paths>
  <external-path name="external_files" path="."/>
  <root-path name="root" path="." />
</paths>

Upvotes: 0

Flyentology
Flyentology

Reputation: 121

The problem with my code was due to change in how files are sent after Android N. Now apps should use content:// instead of file:// so the platform can extend temporary permission for the receing app. Only thing missing is File Provider that will change file:// into content:// in uri.

The whole solution for my problem is here: https://stackoverflow.com/a/38858040/8430049

So code should look like this:

                                    String filePath = songs.get(viewPosition).getPath();
                                    Uri uri = FileProvider.getUriForFile(mContext, "com.simplemusicplayer.fileprovider", new File(filePath));
                                    Intent share = new Intent(Intent.ACTION_SEND);
                                    share.setType("audio/*");
                                    share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                    share.putExtra(Intent.EXTRA_STREAM, uri);
                                    mContext.startActivity(Intent.createChooser(share, "Share Sound File"));

And android manifest needs to include provider:

<provider
        android:authorities="com.simplemusicplayer.fileprovider"
        android:name="android.support.v4.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

Path app will give access to is stored in xml/provider_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

Upvotes: 2

Related Questions