Android - Share file from sd using bluetooth

拥有回忆 提交于 2019-12-12 03:36:51

问题


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 ..."

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)

The 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.


回答1:


I think your file is not release by File-I/O.

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

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

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..

like,

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


来源:https://stackoverflow.com/questions/12157965/android-share-file-from-sd-using-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!