在Android N中通过蓝牙opp共享文件

在Android N中通过蓝牙opp共享文件,android,android-intent,android-contentprovider,android-bluetooth,android-sharing,Android,Android Intent,Android Contentprovider,Android Bluetooth,Android Sharing,我正在尝试的是通过蓝牙共享一个文件。我尝试了以下两种方法将文件名传递给您想要发送的操作。共享活动弹出,当我触摸连接的蓝牙设备时,我得到一个祝酒词,说蓝牙共享:文件未知文件未发送。这两种方法都失败了 public void pushFileOverOpp(String filename) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setPackage("com.a

我正在尝试的是通过蓝牙共享一个文件。我尝试了以下两种方法将文件名传递给您想要发送的操作。共享活动弹出,当我触摸连接的蓝牙设备时,我得到一个祝酒词,说蓝牙共享:文件未知文件未发送。这两种方法都失败了

public void pushFileOverOpp(String filename) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setPackage("com.android.bluetooth");
    intent.setType("audio/mp3");
    File f = new File(Environment.getExternalStorageDirectory(), "images");
    File sample = new File(f, "sample.mp3");
    Uri u = Uri.parse(sample.toString());
    intent.putExtra(Intent.EXTRA_STREAM, u);
    mContext.startActivity(intent);
}
错误,日志-

OppService:URI:/storage/emulated/0/images/sample.mp3 OppService:HINT:null OppService:FILENAME:null OppService:MIMETYPE:audio/mp3

错误,日志-

OppService:URI:content://com.example.com.test.provider/tester/images/sample.mp3 OppService:HINT:null OppService:FILENAME:null


我已经检查了android源代码,当文件名为空时会出现这个错误。日志还显示文件名为空。但我想不出确切的原因。有人能帮我一下吗,我的代码有什么问题。

请参考此代码,它可以使用createChooser方法工作并共享文件

          ArrayList<Uri> arrayList2 = new ArrayList<>();

            String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() +
                    "/NewCallLogs/audio.mp3");

            File files = new File(MEDIA_PATH);
            Uri u = Uri.fromFile(files);
            arrayList2.add(u);

            Intent share = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            share.setData(Uri.parse("mailto:"));
            share.setType("audio/mpeg");
            share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2);
            try {
                startActivity(Intent.createChooser(share, "Share..."));
               // getActivity().finish();
                Log.i("Finished sharing.", "");
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(getActivity(), "nothing shared.", Toast.LENGTH_SHORT).show();
            }
用于仅在蓝牙中共享文件

  ArrayList<Uri> arrayList2 = new ArrayList<>();

            String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() +
                    "/NewCallLogs/audio.mp3" );

            File files = new File(MEDIA_PATH);
            Uri u = Uri.fromFile(files);
            arrayList2.add(u);

            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setData(Uri.parse("mailto:"));
            share.setType("audio/mpeg");
            share.setPackage("com.android.bluetooth");
            share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2);
            startActivity(share);

经过一些研究,我明白了这个问题。有两个问题-

xml文件中外部存储器/sdcard/目录的xml标记错误

我改变如下

    <root-path
    name="root"
    path="/" />
使用以上代码行修改后,文件共享工作正常

完整工作代码-

public boolean pushFileOverOpp(String filename) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("*/*"); // supports all mime types
    intent.setPackage("com.android.bluetooth"); //bluetooth package name, default opp

    File folder = new File(Environment.getExternalStorageDirectory(), "images");
    File file = new File(folder, filename);
    if (!file.exists()) {
        Logger.e("No such file " + filename + " exists!");
        return false;
    }
    Uri u = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);
    intent.putExtra(Intent.EXTRA_STREAM, u);

    mContext.grantUriPermission("com.android.bluetooth", u,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

    Logger.d("Sharing file over bluetooth " + folder.toString());
    mContext.startActivity(intent);
    return true;
}

谢谢。

如果您有任何疑问,请通知我。谢谢,但这在SDK版本24或更高版本上不起作用。这就是为什么我把安卓N放在问题中。上述代码存在两个问题-java.lang.ClassCastException:java.util.ArrayList无法强制转换为android.os.Parcelable-传递ArrayList的parcellable isntead。下一个原因:android.os.FileUriExposedException:-我们不能使用Uri.fromFile.:
mContext.grantUriPermission("com.android.bluetooth", u,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); 
public boolean pushFileOverOpp(String filename) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("*/*"); // supports all mime types
    intent.setPackage("com.android.bluetooth"); //bluetooth package name, default opp

    File folder = new File(Environment.getExternalStorageDirectory(), "images");
    File file = new File(folder, filename);
    if (!file.exists()) {
        Logger.e("No such file " + filename + " exists!");
        return false;
    }
    Uri u = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file);
    intent.putExtra(Intent.EXTRA_STREAM, u);

    mContext.grantUriPermission("com.android.bluetooth", u,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

    Logger.d("Sharing file over bluetooth " + folder.toString());
    mContext.startActivity(intent);
    return true;
}