1

I am trying to share a sound file when a button is long-pressed. This is my code:

public class Tab2 extends Fragment{

Button button1;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_2,container,false);

    button1 = (Button) v.findViewById(R.id.button1);
    button1.setLongClickable(true);

    button1.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("audio/*");

            Uri uri = Uri.parse("android.resource://test.testapp/raw/" + R.raw.sound1);
            share.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(Intent.createChooser(share, "Deel geluidje"));

            return true;
        }

    });



    return v;
  }



}

However, when I long press the button, the share menu comes up, but I can only share the sound with WhatsApp. Any other app doesn't work. (Gmail gives me a message saying ''can't attach empty file''. The sound is in .ogg format, and I have tried converting it to .wav, but it gave me the same problem. What am I doing wrong?

Spickle
  • 47
  • 10

1 Answers1

0

did you try to set Subject and Text After setType

  intent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
  intent.putExtra(Intent.EXTRA_TEXT, "Email Message Body");
  intent.putExtra(Intent.EXTRA_EMAIL,new String[] { "sendTo@gmail.com" });
S. Alawadi
  • 144
  • 1
  • 6
  • Doesn't work, it still says ''Can't attach empty file'' Besides, that wouldn't fix it for other apps like Google Drive – Spickle Nov 01 '15 at 09:21
  • I succesfully send the file to a whatsapp chat and it worked perfectly – Spickle Nov 01 '15 at 12:52
  • look i know its not a solution but try another device ,, because the code is work fine – S. Alawadi Nov 01 '15 at 13:01
  • The code works fine yes, but it doesnt attach the file to the email. It only sets a subject and a message. – Spickle Nov 01 '15 at 13:25
  • worst case is to use code for copy the file from raw file to the SD card and then share it ,,, and again delete it from SD card see this answer http://stackoverflow.com/questions/7976141/get-uri-of-mp3-file-stored-in-res-raw-folder-in-android – S. Alawadi Nov 01 '15 at 14:52