首页 > 解决方案 > 通过 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);

使用上面的代码,如果我将联系号码保存到我的设备中,实际会发生这种情况:

图 1 图 2

通过这个,我可以将 PDF 发送给我保存的联系人,但不能发送给未保存的联系人。

标签: androidandroid-intentwhatsapp

解决方案


**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();
        }
    }

推荐阅读