单一意图共享音频和文本

时间:2018-07-27 10:25:13

标签: android android-intent android-debug android-sharing

嗨,如果有人可以帮助我,那将对我有很大帮助。我一直在尝试找到一种使用Intent与文本共享音频文件的解决方案。我只是想独自在Whatsapp上分享它。如果你们中的任何人都可以抽出时间来帮助我解决这个问题,将不胜感激。当我运行代码时,声音是共享的。没有文字。 这是我的代码:

   public void buttonClick(View v) {
    try {

        String a = copyFiletoExternalStorage(R.raw.accio, "accio.mp3");

        String shareBody = "Here is the share content body";


        Intent shareMedia = new Intent(Intent.ACTION_SEND);
        //set WhatsApp application.
        shareMedia.setPackage("com.whatsapp");
        shareMedia.setType("*/*");
        //set path of media file in ExternalStorage.
        shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(a));
        shareMedia.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        shareMedia.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

        startActivity(Intent.createChooser(shareMedia, "Compartiendo archivo."));
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Whatsapp no se encuentra instalado", Toast.LENGTH_LONG).show();
    }
}

我希望用户仅出于一种目的共享声音和文本。

2 个答案:

答案 0 :(得分:0)

尝试使用Intent.ACTION_SEND_MULTIPLE代替Intent.ACTION_SEND

    Intent shareMedia = new Intent(Intent.ACTION_SEND_MULTIPLE);
    //set WhatsApp application.
    shareMedia.setPackage("com.whatsapp");
    shareMedia.setType("*/*");

    String[] extraMimeTypes = {"audio/*", "image/*"};
    shareIntent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeTypes);
    shareIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

    shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(a));
    shareMedia.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
    shareMedia.putExtra(Intent.EXTRA_TEXT, shareBody);

    startActivity(Intent.createChooser(shareMedia, "Compartiendo archivo."));

答案 1 :(得分:0)

首先,您需要添加provider_paths.xml

see this video to add file provider

我的文件提供商

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

然后将该行添加到AndroidManifest.xml

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

然后使用此代码共享您的语音和文本

Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "some text ... ");
        sendIntent.setType("text/plain");
        if (hasReadExternalStoragePermission()) {
          Uri uriImage = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "voice.wav"));
          sendIntent.putExtra(Intent.EXTRA_STREAM, uriImage);
          sendIntent.setType("audio/*");
        }
        sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(Intent.createChooser(sendIntent, "share"));

我希望它会有所帮助