Android的 - 使用SD蓝牙共享文件 [英] Android - Share file from sd using bluetooth

查看:166
本文介绍了Android的 - 使用SD蓝牙共享文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从我的 SD 使用蓝牙发送文件。我使用的是共享的意图,我想从我的SD发送文件( MP3 )。好吧,当我打开共享菜单,我可以发送文件到电子邮件,Dropbox的,WhatsApp的,但如果我选择蓝牙,我的设备显示消息文件空未发送至...

Im trying to send a file from my SD using Bluetooth. I'm using Share intent, I wanna send a file from my SD (.mp3). ok when I open the share menu, I can send file to email, dropbox, whatsapp, but if I select Bluetooth, My device shows a message "File null was not sent to ..."

我的步骤是: 1.创建送交意愿。 2.从RES我的文件/生到SD 3.加入我的文件putExtra 4.删除文件(就是时间的文件)

My steps are: 1. Create SEND intent. 2. Copy my file from res/raw to SD 3. Add my file to putExtra 4. Delete the file (is temporal file)

在code:

Intent shareIntent=new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("audio/mp3");
        //Copiamos archivo a compartir en la sd
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String fileName = sonidoActual+"-temp.mp3";

        File newSoundFile = new File(baseDir, fileName);

        try {
            byte[] readData = new byte[1024*500];
            InputStream fis = getResources().openRawResource(contexto.getResources().getIdentifier(sonidoActual,"raw", contexto.getPackageName()));
            FileOutputStream fos = new FileOutputStream(newSoundFile);
            int i = fis.read(readData);

            while (i != -1) {
                fos.write(readData, 0, i);
                i = fis.read(readData);
            }

            fos.close();
        } catch (IOException io) {
        }

        ////
        shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse(newSoundFile.getAbsolutePath())/*Uri.parse("file:///sdcard/"+fileName)*//*Uri.parse("android.resource://com.genaut.instantbuttonsfreak/raw/"+texto)*/);
        startActivity(Intent.createChooser(shareIntent,getString(R.string.share)));
        //
        newSoundFile.delete();

任何人都可以帮助我?我读了很多,但没有找到工作的方法,对不起我的英语水平。

Anybody can help me with this? I read a lot but not found a working method, sorry my english.

推荐答案

我觉得你的文件不被 release文件I / O

所以..尝试的flush()然后,FileOutputStream ..像,

SO.. try flush() the FileOutPutStream.. like,

fos.flush();
fos.close();

然后,使用 Uri.fromFile(档案文件)的URI传递意图。但通过URI来意图之前只检查文件是否存在或不..

then, use Uri.fromFile(File file) for uri to pass with Intent.. But before passing Uri to Intent just check whether file is exist or not..

喜欢,

if(newSoundFile.exist())
{
 shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(newSoundFile))
 startActivity(Intent.createChooser(shareIntent,getString(R.string.share)));
 newSoundFile.delete();
}

这篇关于Android的 - 使用SD蓝牙共享文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆