Rename URI file to share audio from raw folder

2019-08-28 22:15发布

I'm trying to make an application with sharing of children that are inside the "raw" folder of the application, but I'm not getting it. The file is shared but does not have an .mp3 extension. I was not able to hear Windows Media Player, but I put a .mp3 extension manually. Does anybody have any idea how I do it to get .mp3 extension automatically? Transform / rename a URI into mp3 file.

The name of the song comes as it is registered in the file, I believe that if it was possible to rename adding the .mp3 in the end may work. But I'm not able to rename.

I am using this code:

Intent share;

Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/raw/name_musica"); 
share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri); share.setType("audio/*"); 
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
startActivity(Intent.createChooser(share, "Enviar via: "));

1条回答
来,给爷笑一个
2楼-- · 2019-08-28 22:38

The documentation for EXTRA_STREAM states that it is:

A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.

android.resource: is not content:. There is no requirement for anything that responds to ACTION_SEND to know what to do with an android.resource: Uri in EXTRA_STREAM.

Copy the resource to the local filesystem and use FileProvider, so FileProvider can give you a content: Uri to use with EXTRA_STREAM. Or, write your own ContentProvider that serves the content directly from your raw resource, and use a content: Uri for your provider.

Also, do not use audio/*. Use the actual concrete MIME type of the content that you are sharing.

查看更多
登录 后发表回答