android - 通过 Android 应用程序将 PDF 文件发送到 WhatsApp 上未保存的联系人号码
问题描述
我想通过我的应用程序向 WhatsApp 上未保存的联系人号码发送 PDF 文件。
我尝试了以下代码:
File outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "sample.pdf");
String message = "Hi test!!!";`
String toNumber = "+910000011111";`
toNumber = toNumber.replace("+", "").replace(" ", "");
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(outputFile));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(toNumber) + "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("image/png");
mContext.startActivity(sendIntent);
使用上面的代码,如果我将联系号码保存到我的设备中,实际会发生这种情况:
通过这个,我可以将 PDF 发送给我保存的联系人,但不能发送给未保存的联系人。
解决方案
**Note: first Save the contact and then share pdf file**
public static void shareWhatsApp(Activity context, String toNumber, String patientName, String filename) {
if (!ContactUtils.isContactPermission(context))
return;
if (Validator.isEmptyString(toNumber)) {
Toast.makeText(context, "Phone number not found.", Toast.LENGTH_SHORT).show();
return;
}
if (!ContactUtils.contactExists(context, toNumber)) {
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
ops.add(ContentProviderOperation.newInsert(
ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
if (patientName != null) {
ops.add(ContentProviderOperation.newInsert(
ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, patientName).build());
}
if (toNumber != null) {
ops.add(ContentProviderOperation.
newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, toNumber)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
}
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
toNumber = toNumber.replace("+", "").replace(" ", "");
toNumber = "91" + toNumber;
File whatsAppFile = new File(Environment.getExternalStorageDirectory() + AppConstants.CONSTANTVALUES.FOLDER_PATH + filename);
if (whatsAppFile.exists()) {
Intent sendIntent = new Intent("android.intent.action.MAIN");
Uri apkURI = FileProvider.getUriForFile(context.getApplicationContext(), context.getApplicationContext().getPackageName() + ".provider", whatsAppFile);
sendIntent.putExtra(Intent.EXTRA_STREAM, apkURI);
sendIntent.putExtra("jid", toNumber + "@s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, "");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("*/*");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(sendIntent);
} else {
Toast.makeText(context.getApplicationContext(), "File not found.", Toast.LENGTH_SHORT).show();
}
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
Toast.makeText(context, "WhatsApp not installed.", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, context.getResources().getString(R.string.error_message), Toast.LENGTH_SHORT).show();
}
}
推荐阅读
- python - 关于嵌套在字典中的列表的这两段代码有什么区别?
- python - 如何建立抛硬币三次的贝叶斯模拟模型
- hive - 用于 hive-hbase 集成的 Hive 列的复合键
- php - 通过 api 添加带有 child_attachments 的 Facebook 帖子时出现“无法使用 URL”错误
- firefox - vuetify 设置后在 Firefox 上显示空白页
- javascript - 字符串转换为整数
- mysql - 从mysql中的unix格式转换后比较两个日期
- spring-boot - 无法解析“javax.persistence.EntityManager”的 bean
- sparql - GraphDB Free 8.8 Sparql INSERT 返回 400。不支持 INSERT?
- node.js - 如何使用 azure bot 框架向 Skype 组发送通知消息?