Search code examples
androidandroid-intentandroid-fileandroid-sharing

Not able to share file with whitespace in name in android


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?


Solution

  • 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"));