今天做了最后分享的功能,组员之间整合了代码,测试各部分的功能实现。
public void shareNote (Note note) { String titleText = note.getTitle(); String contentText = titleText + System.getProperty("line.separator") + note.getContent(); Intent shareIntent = new Intent(); // Prepare sharing intent with only text if (note.getAttachmentsList().isEmpty()) { shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setType("text/plain"); // Intent with single image attachment } else if (note.getAttachmentsList().size() == 1) { shareIntent.setAction(Intent.ACTION_SEND); Attachment attachment = note.getAttachmentsList().get(0); shareIntent.setType(attachment.getMime_type()); shareIntent.putExtra(Intent.EXTRA_STREAM, FileProviderHelper.getShareableUri(attachment)); // Intent with multiple images } else if (note.getAttachmentsList().size() > 1) { shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); ArrayList<Uri> uris = new ArrayList<>(); // A check to decide the mime type of attachments to share is done here HashMap<String, Boolean> mimeTypes = new HashMap<>(); for (Attachment attachment : note.getAttachmentsList()) { uris.add(FileProviderHelper.getShareableUri(attachment)); mimeTypes.put(attachment.getMime_type(), true); } // If many mime types are present a general type is assigned to intent if (mimeTypes.size() > 1) { shareIntent.setType("*/*"); } else { shareIntent.setType((String) mimeTypes.keySet().toArray()[0]); } shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); } shareIntent.putExtra(Intent.EXTRA_SUBJECT, titleText); shareIntent.putExtra(Intent.EXTRA_TEXT, contentText); startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.share_message_chooser))); }