Форум для тех, кто программирует под Android
Anonymous
Как поделиться PDF-файлом и текстом в WhatsApp с единым намерением в Android?
Сообщение
Anonymous » 02 дек 2024, 13:29
private void ShareFileAndLinkViaIntent() {
Set selectedDispatches = mRetailerHistoryAdapter.getSelectedDispatchItems();
Set selectedOtherItems = mRetailerHistoryAdapter.getSelectedOtherItems();
Код: Выделить всё
try {
// Track events for sharing
for (CustomerHistoryListBean item : selectedDispatches) {
Utils.getInstance().eventTracking(this, "dispatch_share");
}
for (CustomerHistoryListBean item : selectedOtherItems) {
Utils.getInstance().eventTracking(this, item.getActivity_type() + "_share");
}
StringBuilder shareText = new StringBuilder();
// Add links and documents for dispatch transactions
for (CustomerHistoryListBean dispatchItem : selectedDispatches) {
String dispatchLink = getShareLink(dispatchItem);
shareText.append(AppConstants.DEEP_LINK_TYPE.DISPATCH).append(": ").append(dispatchLink).append("\n\n");
}
// Add links for other transactions
for (CustomerHistoryListBean otherItem : selectedOtherItems) {
String otherLink = getShareLink(otherItem);
String activityName = getActivityDeeplinkType(otherItem);
shareText.append(activityName).append(": ").append(otherLink).append("\n\n");
}
Intent shareIntent = new Intent();
if (!dispatchFilesUri.isEmpty()) {
// Handle multiple files
if (dispatchFilesUri.size() > 1) {
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.setType("application/pdf");
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, dispatchFilesUri);
} else {
// Single file
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
shareIntent.putExtra(Intent.EXTRA_STREAM, dispatchFilesUri.get(0));
}
} else {
// Text-only sharing
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
}
// Add shared text
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText.toString());
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_link_ex)));
} catch (Exception e) {
e.printStackTrace();
}
}
Этот код отлично работает для Gmail, но не работает для WhatsApp.
Подробнее здесь:
https://stackoverflow.com/questions/792 ... in-android
1733135393
Anonymous
private void ShareFileAndLinkViaIntent() { Set selectedDispatches = mRetailerHistoryAdapter.getSelectedDispatchItems(); Set selectedOtherItems = mRetailerHistoryAdapter.getSelectedOtherItems(); [code] try { // Track events for sharing for (CustomerHistoryListBean item : selectedDispatches) { Utils.getInstance().eventTracking(this, "dispatch_share"); } for (CustomerHistoryListBean item : selectedOtherItems) { Utils.getInstance().eventTracking(this, item.getActivity_type() + "_share"); } StringBuilder shareText = new StringBuilder(); // Add links and documents for dispatch transactions for (CustomerHistoryListBean dispatchItem : selectedDispatches) { String dispatchLink = getShareLink(dispatchItem); shareText.append(AppConstants.DEEP_LINK_TYPE.DISPATCH).append(": ").append(dispatchLink).append("\n\n"); } // Add links for other transactions for (CustomerHistoryListBean otherItem : selectedOtherItems) { String otherLink = getShareLink(otherItem); String activityName = getActivityDeeplinkType(otherItem); shareText.append(activityName).append(": ").append(otherLink).append("\n\n"); } Intent shareIntent = new Intent(); if (!dispatchFilesUri.isEmpty()) { // Handle multiple files if (dispatchFilesUri.size() > 1) { shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.setType("application/pdf"); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, dispatchFilesUri); } else { // Single file shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setType("application/pdf"); shareIntent.putExtra(Intent.EXTRA_STREAM, dispatchFilesUri.get(0)); } } else { // Text-only sharing shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setType("text/plain"); } // Add shared text shareIntent.putExtra(Intent.EXTRA_TEXT, shareText.toString()); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(Intent.createChooser(shareIntent, getString(R.string.share_link_ex))); } catch (Exception e) { e.printStackTrace(); } } [/code] Этот код отлично работает для Gmail, но не работает для WhatsApp. Подробнее здесь: [url]https://stackoverflow.com/questions/79243690/how-to-share-both-pdf-and-text-on-whatsapp-in-single-intent-in-android[/url]
0 Ответы
45 Просмотры
Последнее сообщение Anonymous
28 фев 2024, 13:55
0 Ответы
6 Просмотры
Последнее сообщение Anonymous
19 фев 2025, 16:58
0 Ответы
1 Просмотры
Последнее сообщение Anonymous
02 сен 2025, 11:49
0 Ответы
3 Просмотры
Последнее сообщение Anonymous
03 сен 2025, 18:14
0 Ответы
0 Просмотры
Последнее сообщение Anonymous
05 сен 2025, 00:12