简体   繁体   English

如何将音频文件共享到多个应用程序

[英]How to share audio file to multiple apps

String sharePath = dataSavingModels.get(pos).getPathOfRecording();

Uri uri = Uri.parse(sharePath);
Intent share = new Intent(Intent.ACTION_SEND);
share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_TEXT, dataSavingModels.get(pos).getTextSaved());
share.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(share, "Share Sound File"));

This is the code that I am using to send an audio file, it only sends audio files to WhatsApp, and no other app like Messenger, or Telegram or KakaoTalk 这是我用来发送音频文件的代码,它仅将音频文件发送到WhatsApp,而没有其他应用程序(例如Messenger,Telegram或KakaoTalk)

We create our own class inheriting FileProvider in order to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies. 我们创建继承FileProvider的类,以确保FileProvider与导入的依赖项中声明的FileProvider不冲突。 First create class with name GenericFileProvider like this: 首先创建名称为GenericFileProvider类,如下所示:

import android.support.v4.content.FileProvider;

public class GenericFileProvider extends FileProvider {
}

Then create provider_paths.xml inside res/xml folder. 然后在res/xml文件夹中创建provider_paths.xml Folder may be needed to created if it doesn't exist. 如果文件夹不存在,则可能需要创建文件夹。 The content of the file is shown below. 该文件的内容如下所示。 It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files .: 它描述了我们想共享对外部存储在根文件夹(path=".") ,其名称为external_files。:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
</paths>

Now in manifest inside application tag, add this lines: 现在在application标记的清单中,添加以下行:

       <provider
            android:name=".GenericFileProvider"
            android:authorities="${applicationId}.my.package.name.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

Then Try this code: 然后尝试以下代码:

Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("audio/*");
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".my.package.name.provider", new File(Objects.requireNonNull(sharePath))));
        startActivity(Intent.createChooser(share, "Share Sound File"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM