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

时间:2017-02-21 16:15:34

标签: android android-intent android-contentprovider android-bluetooth android-sharing

我想要的是通过蓝牙共享文件。我尝试了以下两种方法将文件名传递给ACTION_SEND打算。 share活动正在弹出,当我触摸连接的蓝牙设备时,我会举杯祝贺Bluetooth share: File Unknown file not sent。这两种方法都失败了。

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:提示:null   OppService:FILENAME:null   OppService:MIMETYPE:audio / mp3

File f = new File(mContext.getFilesDir(), "images");
File sample = new File(f, "sample.mp3");
Uri u = FileProvider.getUriForFile(mContext,
           BuildConfig.APPLICATION_ID + ".provider", sample);
intent.putExtra(Intent.EXTRA_STREAM, u);

错误,记录 -

  

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

我已经检查了android源代码,当filename为null时会出现此错误。 Log还说filename是null。但我无法弄清楚具体原因。有人可以帮帮我,我的代码出了什么问题。

2 个答案:

答案 0 :(得分:0)

请参考此代码,它可以使用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);

答案 1 :(得分:0)

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

  

外部存储的xml标记(/ sdcard /)目录在xml文件中是错误的。

我改变如下。

    <root-path
    name="root"
    path="/" />
  

未授予URI权限

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

感谢。