如何通过意图在原始文件中发送音频

时间:2014-08-01 10:36:34

标签: android bluetooth

我的应用中有很多音轨,我想通过蓝牙与其他设备分享。我已经阅读了所有的问题和答案,但我仍然有问题。通过蓝牙发送时,设备会显示一条消息 -

  

"文件未知。文件未发送.."

我不知道问题是什么?请帮帮我..

代码

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button send=(Button) findViewById(R.id.button_send);  
    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Uri imageUri = Uri.parse("android.resource:// com.example.bluthooth/raw/aaa.mp3");
              Intent intent = new Intent(Intent.ACTION_SEND);
              intent.setPackage("com.android.bluetooth");
              intent.setType("audio/*");

              intent.putExtra(Intent.EXTRA_STREAM, imageUri);
              startActivity(Intent.createChooser(intent , "Share"));

        }
    });

     }
   }

我在aaa.mp3文件夹中有一条名为raw的曲目。

2 个答案:

答案 0 :(得分:2)

File audio = new File("/path/to/audio.mp3");
Intent intent = new Intent(Intent.ACTION_SEND).setType("audio/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(audio));
startActivity(Intent.createChooser(intent, "Share to"));

答案 1 :(得分:-1)

解析音频文件的方式是错误的。

File newSoundFile = new File(baseDirectory, fileName);

byte[] readData = new byte[1024*500];
InputStream inputstream = getResources().openRawResource(context.getResources().getIdentifier(filename,"raw", context.getPackageName()));
FileOutputStream outputstream = new FileOutputStream(newSoundFile);
int i = inputstream.read(readData);

while (i != -1) {
    outputstream.write(readData, 0, i);
    i = inputstream.read(readData);
}
outputstream.flush();
outputstream.close();

intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(soundfilepath));
//can share in any way - to other applications as well as bluetooth
startActivity(Intent.createChooser(shareIntent,getString(R.string.share)));
//delete the copy
newSoundFile.delete();

请在此处Android - Share file from sd using bluetooth

查看此答案