将音乐文件从一个应用程序发送到另一个应用

时间:2023-01-27 23:26:21

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.

我正在尝试使用此代码将应用程序中的音乐文件发送给其他人。我已经启用了WRITE_EXTERNAL_STORAGE权限。但每当我选择应用程序时,我想与它共享我的文件没有出现或我得到Toast说格式无法识别。

                                    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

我从filePath获取的路径例如是这样的:/ storage / emulated / 0 / Music / 2Pac - 更好的dayz - tupac - 更好的days.mp3

2 个解决方案

#1


2  

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.

我的代码的问题是由于Android N之后文件的发送方式发生了变化。现在应用程序应该使用content://而不是file://因此平台可以扩展临时应用程序的临时权限。唯一缺少的是文件提供程序,它将把文件://更改为内容://在uri中。

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

我的问题的整个解决方案是:https://*.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:

并且android manifest需要包含提供者:

<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

Path app将提供访问权限存储在xml / provider_paths.xml中

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

#2


0  

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

从@Flyentology的答案我不得不做一点改变才能让它发挥作用。

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

#1


2  

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.

我的代码的问题是由于Android N之后文件的发送方式发生了变化。现在应用程序应该使用content://而不是file://因此平台可以扩展临时应用程序的临时权限。唯一缺少的是文件提供程序,它将把文件://更改为内容://在uri中。

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

我的问题的整个解决方案是:https://*.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:

并且android manifest需要包含提供者:

<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

Path app将提供访问权限存储在xml / provider_paths.xml中

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

#2


0  

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

从@Flyentology的答案我不得不做一点改变才能让它发挥作用。

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