问题
I am working on an Android application that is already successfully sharing a generated PDF file via Bluetooth using the following method:
public static void sharePdfFile(Context ctx, String pathAndFile) {
try {
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage("com.android.bluetooth");
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile));
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
I have been asked to include a second file (CSV format) in this share intent so that both files are sent together. I immediately found this question, which addresses sending multiple files via Bluetooth, but only using files of the same MIME type ("video/*" in that example.)
I have found plenty of examples of wildcard MIME subtypes ("video/*", "text/*", etc.) but at this point I been unable to find any examples of an Intent with more than one specific MIME type set (example: "application/pdf" and "text/comma-separated-values"). So, I created a test method using "*/*" as the MIME type hoping that would do the trick. Unfortunately, my test method is not even getting far enough to activate the Bluetooth share popup to select a nearby device.
I am not sure what I am doing wrong or have left out. I cannot seem to trap any errors while debugging so I assume I'm still missing something. I do know the PDF and CSV files and their respective URI's are OK because both files transmit fine through the original method (I changed the MIME type and URI on the existing method to test the new CSV file.)
Here is my test method:
public static void shareTwoFilesTest(Context ctx, String pathAndFile, String pathAndFile2) {
try {
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setPackage("com.android.bluetooth");
share.setType("*/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile));
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathAndFile2));
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
回答1:
I ended up finding a working solution to my question/issue just about right after I finished my final draft. I kept researching the issue while writing and found this answer that indicated I was missing intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
in my test method. I added that but my test method still didn't work.
I then found this answer indicating that maybe I should add an array list of URI's to my intent rather than trying to add multiple single URI's. After adding this final missing piece I ended up with a working test function that I can now implement officially:
public static void shareTwoFilesTest(Context ctx, String pathAndFile, String pathAndFile2) {
ArrayList<Uri> Uris = new ArrayList<>();
Uris.add(Uri.parse(pathAndFile));
Uris.add(Uri.parse(pathAndFile2));
try {
Intent share = new Intent(Intent.ACTION_SEND_MULTIPLE);
share.setPackage("com.android.bluetooth");
share.setType("*/*");
share.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
share.putExtra(Intent.EXTRA_STREAM, Uris);
share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(share);
} catch (Exception e) {
ExceptionDAO.Log(CATEGORY.SHARE_INTENT, e, ctx, e.getMessage(), true);
}
}
If you think this answer could be improved or have another way of solving the issue, please feel free to answer or comment as you see fit. I went ahead and posted the question and my answer so that hopefully this can help someone else in the future.
来源:https://stackoverflow.com/questions/46352498/how-to-programmatically-share-multiple-files-of-different-mime-types-within-the