代码之家  ›  专栏  ›  技术社区  ›  Sujeet Kumar Mehta

如何在android中通过whatsapp共享pdf和文本?

  •  18
  • Sujeet Kumar Mehta  · 技术社区  · 8 年前

    我尝试了以下代码,但它没有附加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);
    
    10 回复  |  直到 8 年前
        1
  •  35
  •   KHALED    8 年前

    我遇到了一个问题,当时我试图从资产文件夹打开一个pdf文件,但我没有成功,但当我试图从下载文件夹(例如)打开时,它确实成功了,下面是一个例子:

    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);      
    
        2
  •  24
  •   Idris Bohra    7 年前

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

    第1步: 在AndroidManifest中添加FileProvider标记。application标记下的xml。

    <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>
    

    第二步:

    然后创建providerpath。xml文件位于res文件夹下的xml文件夹中。如果文件夹不存在,则可能需要创建该文件夹。文件内容如下所示。它描述了我们希望以External_files的名称共享对根文件夹(path=“.”)处外部存储的访问。

    <?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);
    

    希望这将有助于:)

        3
  •  6
  •   Dishant Kawatra    6 年前
           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
  •  4
  •   Aalishan Ansari    4 年前

    这对我来说很有用。

     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
  •  1
  •   Rahul Khurana steve    8 年前

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

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

    我认为 操作_发送 这个意图意味着“发送到其他应用程序”,而不是“发送到别的地方”。

        6
  •  1
  •   Osvel Alvarez Jacomino    5 年前

    我用过 FileProvider 因为这是一种更好的方法。

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

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

    然后您需要添加 provider 在您的 manifests

    <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
  •  0
  •   Rishabh Dutt Sharma    8 年前

    尝试添加 意向.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
  •  0
  •   KHALED    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
  •  0
  •   ARAVIND RAJ    5 年前

    尝试以下代码

    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
  •  -4
  •   lomkrodsp    8 年前

    转到android中的文件管理器应用程序 然后打开它 然后转到>>>数据>>>数据>>>通用域名格式。whatsapp然后>>>共享_引用 打开com.whatsapp_preference。xml文件 搜索并选择文件>>>>name=文档pdf….</字符串>并保存此文件 之后>>>设置>>>>应用程序>>>>whatsapp>>>>并按下强制停止