0

I am not able to share audio file in whatsapp if there is any whitespace in the filename. But it works when sharing using email client. For filenames without spaces also it works fine. Below is the code which I am using

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
File F = new File(filePath);
F.setReadable(true, false);
Uri fileURI = Uri.fromFile(F);
Log.e("Share", "Share file url is " + fileURI);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Shared file");
sharingIntent.setType("*/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileURI);

I tried doing filePath.replace(" ", "\ "), not working. What changes should be done to share the file?

Ratan
  • 1,747
  • 2
  • 18
  • 27

2 Answers2

1

This works when you trying to share with WhatsApp an audio file with whitespace:

String filePath = "file:///sdcard/Download/example attachment.mp3";

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(filePath));
shareIntent.setType("audio/*");
startActivity(Intent.createChooser(shareIntent, "Share audio file"));
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
  • Using this I was able to share audio in whatsapp but was not able to send the file using email client. I changed the type in my code to ("audio/*") and it started working in both. – Ratan May 01 '15 at 12:03
  • @Ratan Can I ask which email client you use? Because with Gmail this code works correctly – Mattia Maestrini May 01 '15 at 12:51
  • I was using gmail. Did you try with a filename containing spaces? Check if the receiver receives the file. I was not able to send it though it was showing file attached. – Ratan May 01 '15 at 13:44
  • Yes i tried to send an attachment with a filename containing spaces and the recipient receives correctly the mail with attachment. I just edited the answer to add the filePath variable to clarify the code. – Mattia Maestrini May 01 '15 at 15:28
  • I'm currently observing the same issue, and eg. sharing to Google Drive works, but not to Google Photos. I guess it depends on how the receiving activity parses the Uris, and thus really the code quality of the receiving app. Seems Google Drive programmers have better test coverage than their Google Photo colleagues :) – Simon Dec 30 '18 at 20:14
0

I was able to share audio using same code as I have posted with just a minor change in the type. Below code works for both email as well as whatsapp sharing:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
File F = new File(filePath);
F.setReadable(true, false);
Uri fileURI = Uri.fromFile(F);
Log.e("Share", "Share file url is " + fileURI);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Shared file");
sharingIntent.setType("audio/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileURI);
Ratan
  • 1,747
  • 2
  • 18
  • 27