I saw your answer about sending file through Bluetooth. (answered Jun 13 '11 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 works. It will open a default Bluetooth tool/window/dialog to send a file. But would you please teach me how 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 default Bluetooth tool/window/dialog for 3 times! @@ If there are 10 files, it will open the default Bluetooth tool/window/dialog 10 times!!
May I know how to open the default Bluetooth tool/window/dialog once, then send all files?
Thank you very much in advance!
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!
来源:https://stackoverflow.com/questions/8940583/sending-multiple-files-through-bluetooth