使用Intent Action SEND通过蓝牙发送文件在android 4.2.2中不起作用



我使用Intent SEND通过蓝牙共享文件。我在这里添加了我的代码。

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);       
    intent.setComponent(new ComponentName("com.android.bluetooth",
            "com.android.bluetooth.opp.BluetoothOppLauncherActivity"));
    intent.setType("text/plain");
    File file = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/Sample.txt");
     intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    startActivity(intent);

此代码在android版本4.2.2中不起作用。在android verison 4.2.2中运行此代码时,出现异常:

        android.content.ActivityNotFoundException: Unable to find explicit activity   class {com.android.bluetooth/com.android.bluetooth.opp.BluetoothOppLauncherActivity}; have you declared this activity in your AndroidManifest.xml?

但这段代码在android 4.2.X以下运行良好。为什么这段代码不能在android 4.2.2中运行?。

您可以通过蓝牙共享文件,如下所示:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(picURI);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sample.txt");
sharingIntent.setType("text/plain");
sharingIntent.setPackage("com.android.bluetooth");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(sharingIntent, "Share file"));

尝试设置packageName而不是组件

intent.setPackage("com.android.bluetooth");

最新更新