Sending multiple files through Bluetooth

梦想的初衷 提交于 2019-12-04 05:59:40

Well, this can be done by the following means. Let the list of files to be sent be denoted by mMultiSelectData.

ArrayList<Uri> uris = new ArrayList<Uri>();
int length = mMultiSelectData.size();
Intent mail_int = new Intent();
mail_int.setAction(android.content.Intent.ACTION_SEND_MULTIPLE);
mail_int.setType("*/*");
for(int i = 0; i < length; i++) {
    File file = new File(mMultiSelectData.get(i));
    uris.add(Uri.fromFile(file));
}
mail_int.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
mContext.startActivity(mail_int);

This will open a selection box. Choose Bluetooth and the list of files will be sent.

It's quite a simple exercise but this comes with a cost! SDCard storage space.

Yes, you do need to dump the multiple files onto the SDCard for the purpose.

For each file dumped into the SDCard you need to build up an array list of Uri.

ArrayList<Uri> listDumpedFileUris = new ArrayList<Uri>();
Uri uriFile = Uri.fromFile(new File(dumpedFilePath));
listDumpedFileUris.add(uriFile);

The crucial part is to explicitly tell the intent that the chooser must be able to read the dumped files on the SDCard by way of granting the read permission, and more importantly, add the array list to the intent's extra parcelable bundle.

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, listDumpedFileUris);
startActivity(Intent.createChooser(intent, "Send these files using..."));

Then all files selected will be sent via the Android's bluetooth run-time facility. By the way, you may have to explicitly specify the setType for the files, for example, image/jpeg as in:

intent.setType("image/jpeg");

The only onus is on your part to clean up the remnants of the SDCard file-system which is something, for the most part, android users absolutely loathe!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!