Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

android - Blue-tooth file not sent error

I am trying to send a file from my application on android phone to other devices (they may or may not be be android phones).

my whole code for sending the file is :

try{
            File dir = getCacheDir();
            File f;
            try {
                f = File.createTempFile("card", ".Xcard", dir);

                Intent i = new Intent();
                i.setAction(Intent.ACTION_SEND);
                i.setType("*/*");
                i.putExtra(i.EXTRA_STREAM, Uri.fromFile(f));
                startActivity(i);


            } catch (IOException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();

                e.printStackTrace();
            }

            }catch(Exception e){

                Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
            }

But my file is not being sent to the phone?? What is wrong in the code? Is it because the receiving phone does not recognize my ".Xcard" file?

But I don't think that is the problem because i tried sending "apk" file to the other device and it received even though it doesn't understand the apk file. (i am trying with the non-android phone).

Then why not the file that I am sending being not-sent? is it because it is created in Cache directory?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This worked for me:

String root = Environment.getExternalStorageDirectory().toString();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/html");
File f = new File(root + "/bluetooth/test2.html");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
startActivity(Intent.createChooser(i, "Send page"));

The difference is in create the file in the bluetooth directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...