【问题标题】:How to share pdf and text through whatsapp in android?如何在android中通过whatsapp分享pdf和文本?
【发布时间】:2016-09-16 01:13:27
【问题描述】:

我尝试使用以下代码,但它没有附加 pdf 文件。

Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, message);
        sendIntent.setType("text/plain");
        if (isOnlyWhatsApp) {
            sendIntent.setPackage("com.whatsapp");

        }

        Uri uri = Uri.fromFile(attachment);
        sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
        activity.startActivity(sendIntent);

【问题讨论】:

  • 您是否可以在共享 PDF 文件时添加文本消息或标题以共享意图。我可以共享 pdf,但它不会在我在创建共享意图时使用 Intent.EXTRA_TEXT 编写的 whatsapp 中显示文本。

标签: android pdf share whatsapp


【解决方案1】:

我遇到了这个问题,我试图从 assets 文件夹打开一个 pdf 文件,但我没有工作,但是当我尝试从 Download 文件夹打开时(例如),它确实有效,这是一个例子:

File outputFile = new File(Environment.getExternalStoragePublicDirectory
    (Environment.DIRECTORY_DOWNLOADS), "example.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);      

【讨论】:

  • 此代码对我不起作用。我正在尝试将 pdf 从我的应用程序共享到 watsapp
  • @Vishali,你能找到解决办法吗?
【解决方案2】:

请注意,如果您的 targetSdkVersion 为 24 或更高版本,我们必须使用 FileProvider 类来授予对特定文件或文件夹的访问权限,以便其他应用程序可以访问它们。

第一步:在AndroidManifest.xml的application标签下添加FileProvider标签。

<provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

第 2 步:

然后在 res 文件夹下的 xml 文件夹中创建一个 provider_paths.xml 文件。如果文件夹不存在,可能需要创建它。该文件的内容如下所示。它描述了我们希望共享对位于根文件夹 (path=".") 的外部存储的访问权限,名称为 external_files。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

第 3 步:最后一步是更改下面的代码行

Uri photoURI = Uri.fromFile(outputFile);

Uri uri = FileProvider.getUriForFile(PdfRendererActivity.this, PdfRendererActivity.this.getPackageName() + ".provider", outputFile);

第 4 步(可选)

如果使用意图使系统打开您的文件,您可能需要添加以下代码行:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

希望这会有所帮助:)

【讨论】:

  • 将 Manifest 文件的 Provider 部分中的名称从 android:name="android.support.v4.content.FileProvider" 更改为 android:name="androidx.core.content.FileProvider" SDK...
【解决方案3】:
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                pdfUri = FileProvider.getUriForFile(this, this.getPackageName() + ".provider", pdfFile);
            } else {
                pdfUri = Uri.fromFile(pdfFile);
            }
            Intent share = new Intent();
            share.setAction(Intent.ACTION_SEND);
            share.setType("application/pdf");
            share.putExtra(Intent.EXTRA_STREAM, pdfUri);
            startActivity(Intent.createChooser(share, "Share"));

If you are using Intent.createChooser then always open chooser 

【讨论】:

    【解决方案4】:

    这适用于我的 kotlin 代码。

     var file =
                File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    "${invoiceNumber}.pdf"
                )
            if (file.exists()) {
                val uri = if (Build.VERSION.SDK_INT < 24) Uri.fromFile(file) else Uri.parse(file.path)
                val shareIntent = Intent().apply {
                    action = Intent.ACTION_SEND
                    type = "application/pdf"
                    putExtra(Intent.EXTRA_STREAM, uri)
                    putExtra(
                        Intent.EXTRA_SUBJECT,
                        "Purchase Bill..."
                    )
                    putExtra(
                        Intent.EXTRA_TEXT,
                        "Sharing Bill purchase items..."
                    )
                }
                startActivity(Intent.createChooser(shareIntent, "Share Via"))
    
            }
    

    【讨论】:

      【解决方案5】:

      ACTION_VIEW 用于查看文件。 ACTION_VIEW 将打开可以处理列表中 pdf 文件的应用程序。

      startActivity(new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(reportFile), "application/pdf")));
      

      我认为 ACTION_SEND 意图的意思是“发送到其他应用程序”,而不是明确地“发送到其他地方”。

      【讨论】:

      • 感谢您的回答。让我尝试检查它是否有效。看起来可以,但我不确定whatsapp是否允许这样做。
      • 是的,如果它对您的问题不起作用,您也可以尝试一下:sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportPath));
      【解决方案6】:

      我使用了FileProvider,因为它是一种更好的方法。

      首先,您需要使用您的私有路径配置添加一个 xml/file_provider_paths 资源。

      <paths>
          <files-path name="files" path="/"/>
      </paths>
      

      然后你需要在你的manifests中添加provider

      <provider
          android:name="androidx.core.content.FileProvider"
          android:authorities="cu.company.app.provider"
          android:exported="false"
          android:grantUriPermissions="true">
          <meta-data
              android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/file_provider_paths" />
      </provider>
      

      最后在您的Kotlin 代码中

      fun Context.shareFile(file: File) {
      
          val context = this
      
          val intent = Intent(Intent.ACTION_SEND).apply {
      
      
              //file type, can be "application/pdf", "text/plain", etc
              type = "*/*"
      
              //in my case, I have used FileProvider, thats is a better approach
              putExtra(
                  Intent.EXTRA_STREAM, FileProvider.getUriForFile(
                      context, "cu.company.app.provider",
                      file
                  )
              )
      
              //only whatsapp can accept this intente
              //this is optional
              setPackage("com.whatsapp")
      
          }
      
          try {
              startActivity(Intent.createChooser(intent, getString(R.string.share_with)))
          } catch (e: Exception) {
              Toast.makeText(this, "We can't find WhatsApp", Toast.LENGTH_SHORT).show()
          }
      
      }
      

      【讨论】:

        【解决方案7】:

        尝试添加 Intent.setType 如下:-

            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, message);
            // sendIntent.setType("text/plain");
            if (isOnlyWhatsApp) {
                sendIntent.setPackage("com.whatsapp");
            }
        
            Uri uri = Uri.fromFile(attachment);
            sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
            sendIntent.setType("application/pdf");
            activity.startActivity(sendIntent);
        

        【讨论】:

          【解决方案8】:

          对于共享文本,您可以在下面找到一个很好的示例,如果愿意,您可以在其中与特定号码共享文本!

          public static void openWhatsAppConversation(Activity activity, String number) {
              boolean isWhatsappInstalled = isAppInstalled(activity, "com.whatsapp");
              if (isWhatsappInstalled) {
                  Uri uri = Uri.parse("smsto:" + number);
                  Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
                  sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  sendIntent.setPackage("com.whatsapp");
                  activity.startActivity(sendIntent);
              } else {
                  ToastHelper.show(activity, "WhatsApp is not Installed!");
                  openMarket(activity, "com.whatsapp");
              }
          }
          

          【讨论】:

            【解决方案9】:

            试试下面的代码

            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            
            File pdfFile = new File(Environment.getExternalStoragePublicDirectory
                                   (Environment.DIRECTORY_DOWNLOADS), "Your file");
            Uri uri = Uri.fromFile(pdfFile);
            
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.setType("application/pdf");
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(Intent.createChooser(shareIntent, "Share via")); 
            

            【讨论】:

              【解决方案10】:

              转到 android 中的文件管理器应用程序 并打开它 然后去>>>数据>>>数据>>>com.whatsapp 然后>>>share_prefs 打开 com.whatsapp_preference.xml 文件 搜索并选择文件 >>>>name=document pdf .... 并保存此文件 在>>>设置>>>>应用程序>>>>whatsapp>>>>并按下强制停止后 新的再次打开whatsapp并尝试发送或分享您的文档

              【讨论】:

              • 嗨@lomkrodsp 我的意思是问我们如何通过我们的应用程序使用代码而不是手动完成它
              猜你喜欢
              • 2018-08-07
              • 1970-01-01
              • 2013-03-07
              • 1970-01-01
              • 2021-11-12
              • 2016-07-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多