Sending File via Bluetooth using Intent Action SEN

2020-08-01 08:02发布

问题:

I have used Intent-SEND to share the files via bluetooth.I have add my code here.

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

This code is not working in android version 4.2.2. While running this code in android verison 4.2.2 , getting Exception:

        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?

But this code working fine in below android 4.2.X. why this code is not working in android 4.2.2?.

回答1:

You can share the file by bluetooth like this:

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


回答2:

Try setting the packageName instead of the component

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