1
b1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Uri uri = Uri.parse("android.resource://haydo.kufurbaz.kfrbazhaydosesleri/raw/aaaww.mp3");
            Intent myınIntent = new Intent(Intent.ACTION_SEND);
            myınIntent.setType("audio/*");
            String sharebody = "Your body here";
            myınIntent.putExtra(Intent.EXTRA_STREAM,uri);
            startActivity(Intent.createChooser(myınIntent,"Share using"));
            return true;
        }
    });

When I choose to share it on GMail, for example, it says something like "Failed to attach empty file". Looks like I'm not getting the right file path, so I'm basically sharing nothing. What am I doing wrong?

Any help would be much appreciated.

levi
  • 23
  • 6

1 Answers1

0

Duplicated question

Android Attaching a file to GMAIL - Can't attach empty file

How they solve the issue? by @guifgdeo

Ok, got it to work now, after a lot of research and intercepting some Intents.

What I had to do was change the file:/// to content://.

I did this following this information from Android: https://developer.android.com/reference/android/support/v4/content/FileProvider.html

The only major change was that I used a hard-coded path to /sdcard/file.ext. Also, the line

getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

was changed to

Uri contentUri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", newFile);

Also had to include:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.setData(contentUri);

I do not really understand why I had to change from File to Content, but after this, the file is now being attached again! See the link if you face this issue, and don't forget about the new .xml that needs to be created.