Reputation: 1281
I am trying to share an mp3 file created in my app using the FileProvider. I followed the documentation and this Stack Overflow post, but it is not working: for example, gmail says "Can't attach empty file".
The mp3 files are saved in Environment.getExternalStorageDirectory()+"/MyApp"
.
I added the provider to the manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
Added the file_provider_paths.xml
to res:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="share" path="MyApp/" />
</paths>
And added this method to my activity:
public void ShowShareChooser(String filename)
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
File f = new File(filename);
Uri fileUri = FileProvider.getUriForFile(this,"com.myapp.fileprovider",f);
String type = getContentResolver().getType(fileUri);
shareIntent.putExtra(Intent.EXTRA_STREAM,fileUri);
shareIntent.setType(type);
//shareIntent.setDataAndType(fileUri,type); // another failed attempt
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share song"));
}
When filename
is "/storage/emulated/0/MyApp/Audio.mp3"
, the resulting fileUri
is content://com.myapp.fileprovider/share/Audio.mp3
. The type
is audio/mpeg
.
I am targeting API 22, and testing this on a Nexus 5 (6.0.1).
Upvotes: 1
Views: 1061
Reputation: 1281
As it often turns out, it was a stupid mistake on my part. The file was not created anymore because of a new bug somewhere else in the code, while I assumed it was there. The code works indeed fine, I'll leave it here for future reference (and as a personal memento).
Upvotes: 1