Send multiple files via Bluetooth

I saw your answer about sending a file via Bluetooth. (answered June 13, 2011 at 5:01)

Intent i = new Intent(Intent.ACTION_SEND); i.setType("image/jpeg"); i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/sdcard/file.jpg")); startActivity(Intent.createChooser(i, "Send Image")); 

Yes! It is working. It will open a standard bluetooth tool / window / dialog to send the file. But could you teach me to send more files? Here is my code ...

  String xFile[3] = { "aa.txt", "bb.txt", "cc.txt" }; Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); for (int i = 0; i < 3; i ++) { intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(xFile[i]))); startActivity(intent); } 

It works, but it will open the standard bluetooth tool / window / dialog 3 times! @@ If there are 10 files, it will open the standard Bluetooth tool / window / dialog by default 10 times!

Can I learn how to open the default bluetooth tool / window / dialog by default once, and then send all the files?

Thank you in advance!

+6
source share
2 answers

Well, this can be done using the following tools. Let the list of sent files is indicated by the symbol 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); 

A selection window opens. Select Bluetooth and a list of files will be sent.

+5
source

This is a fairly simple exercise, but it has to do with the cost! Sdcard.

Yes, you need to dump several files on the sdcard for this purpose.

For each file flushed to SDCard, you need to create a list of Uri arrays.

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

The most important part is a direct indication of the intention that the selector should be able to read the dumped files on the SDCard by granting read permission and, more importantly, adding the list of arrays to an optional additional package.

 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 selected files will be sent via the Bluetooth runtime setting for Android. By the way, you may need to explicitly specify setType for files, for example, image/jpeg , as in:

 intent.setType("image/jpeg"); 

The only thing on your part is to clear the remnants of the SDCard file system, which, by and large, is absolutely not suitable for Android users.

+1
source

Source: https://habr.com/ru/post/906468/


All Articles