2

我正在尝试将文件从我在 android 手机上的应用程序发送到其他设备(它们可能是也可能不是 android 手机)。

我发送文件的整个代码是:

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();
            }

但是我的文件没有被发送到手机??代码有什么问题?是不是因为接收手机无法识别我的“.Xcard”文件?

但我认为这不是问题,因为我尝试将“apk”文件发送到另一台设备,即使它不理解 apk 文件,它也收到了。(我正在尝试使用非安卓手机)。

那为什么不发送我发送的文件?是因为它是在 Cache 目录中创建的吗?

4

1 回答 1

0

这对我有用:

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"));

不同之处在于在蓝牙目录中创建文件。

于 2013-07-28T21:27:43.723 回答