0

On Android Pie, I want to share my CSV file to any sharing app such as Bluetooth or email, etc... it runs in lower version but in 9.0 it won't

  final String test = "content://" + Environment.getExternalStorageDirectory() 
                                   + "/Download" + "/" + "ProgressivePayment.csv";
  Intent sharingIntent = new Intent();
  sharingIntent.setAction(Intent.ACTION_SEND);
  sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(test));
  sharingIntent.setType("text/csv");
  startActivity(Intent.createChooser(sharingIntent, "share file with"));
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Allen Oculam
  • 51
  • 1
  • 9

1 Answers1

0

it runs in lower version

It is possible that there is some poorly-written app that incorrectly handles EXTRA_STREAM and accidentally handles that broken Uri. Or, as Martin Zeitler points out, perhaps there is a strange pre-installed ContentProvider on the device that happens to honor your broken Uri. Most apps on most devices will crash when attempting to use that Uri, as there will be no matching ContentProvider.

You can use FileProvider to correctly create a content Uri that will serve up some file that you control.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • without `FileProvider` it might only complain that an `Uri` is being leaked beyond the scope of the application (recently had this problem and can only confirm the suggestion). – Martin Zeitler May 29 '19 at 11:25
  • @MartinZeitler: I expect it to crash with a failed lookup of the authority string. There is no requirement that there be a `ContentProvider` with a `storage` authority that knows how to handle the rest of that. – CommonsWare May 29 '19 at 11:41