【问题标题】:Share pdf file via whatsapp from my app on Android通过我在 Android 上的应用程序中的 whatsapp 共享 pdf 文件
【发布时间】:2017-02-23 13:35:23
【问题描述】:

我正在尝试将 pdf 文件从我的应用程序发送到 whatsapp,这是代码, 但是缺少一些东西!

它打开whatsapp,我可以选择一个联系人,但它说“分享失败”!

代码

String PLACEHOLDER = "file:///android_asset/QUOT_2016_10(test).pdf";
File f = new File(PLACEHOLDER);
Uri uri = Uri.fromFile(f);

Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_TEXT, "hi");
share.setPackage("com.whatsapp");

share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("application/pdf");

activity.startActivity(share);

【问题讨论】:

  • 我解决了这个问题,如果有人遇到同样的问题,这里就是答案。

标签: android pdf whatsapp


【解决方案1】:

我发现了问题,如果有人遇到同样的问题,这里就是答案。问题是我试图从不起作用的资产文件夹中打开 pdf,例如,如果尝试从下载文件夹中打开 pdf,它会起作用。最终正确方法请参考以下代码:

File outputFile = new File(Environment.getExternalStoragePublicDirectory
        (Environment.DIRECTORY_DOWNLOADS), "ref Number from Quotation.pdf");
Uri uri = Uri.fromFile(outputFile);

Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/pdf");
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.whatsapp");

activity.startActivity(share);                                                

【讨论】:

  • 收到错误 The file format is not supported 存储在我设备内部存储的下载文件夹中的 PDF 文件..
  • 如何发送到特定手机号?
  • @PrajwalWaingankar 你知道如何发送特定的手机号码吗?
【解决方案2】:
 File outputPath= new File(Environment.getExternalStorageDirectory() + "/MyPdf/example.pdf");//change with your path

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("application/pdf");
    Uri fileUri = FileProvider.getUriForFile(getApplicationContext(),
            getPackageName() + ".fileprovider", outputPath);
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
    startActivity(Intent.createChooser(shareIntent, "Share it"));

【讨论】:

    【解决方案3】:

    这在技术上是错误的,如果有人有 WhatsApp 业务或想在 gmail 上共享文件,那么使用这个...

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, _text);
    shareIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse(_file));
    startActivity(Intent.createChooser( shareIntent, "Share"));
    

    在这个你只需要添加文本和文件 您附加的文本将成为 gmail 中的主题,如果您在 WhatsApp 上共享图像,则文本将成为图像标题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 2021-10-28
      • 2017-03-29
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      相关资源
      最近更新 更多