0

I'm writing a soundboard app that takes files from res/raw folder and puts them in a gridview with a button for each. If I press a button longer, I can get a menu from which I can share the audio file, but, when I try to send it via WhatsApp, I get an unnamed file without extension. When I look for it in the file manager, I find a file called "DOC-20190722-WA0000." (note the point without extension) and if I rename it adding the mp3 extension, it plays without any problem. I'd prefer to send the file directly instead of saving the file before to sdcard and the sharing it.

The code is adapted from this answer, but, as I said before, it works only partially

In my code I initially get the list of files in the raw folder:

Field[] raw = R.raw.class.getFields();

Then I create a set of buttons in an adapter with the same name of the files in raw and indexed with the same order as the files in the "Field" array. When I long click a button I get the id of the button and save it in a variable:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
   super.onCreateContextMenu(menu, v, menuInfo);
   selectedButton = v;
   getMenuInflater().inflate(R.menu.button_menu,menu);
}

Finally I get the id of the button from which I get the id of the mp3 file in "raw" and I put it in the share intent:

@Override
public boolean onContextItemSelected(MenuItem item) {
    int position = selectedButton.getId();
    switch (item.getItemId()){
        case R.id.share:
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+getApplicationContext().getPackageName()+"/raw/"+raw[position].getName()));
            shareIntent.setType("audio/mp3");
            startActivity(Intent.createChooser(shareIntent,"Condividi con"));
            return true;
        ...
        default:
            return super.onContextItemSelected(item);
    }
}

It should, in theory, send it with its original name, but it doesn't so I'm looking for a way to set the name manually.

0 Answers0