2016-09-16 413 views
3

我试着用下面的代码,但它没有附加pdf文件。如何通过Android中的WhatsApp共享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); 

回答

9

我有这个问题,我在那里试图打开从资产文件夹中的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);  
+0

我在这里有一个相关的问题:https://stackoverflow.com/questions/45901768/how-to-share-pdf-through-whatsapp-to-an-unsaved-contact –

-2

到文件中的Android 经理的应用程序,并打开它 然后进入>>>数据>>>数据>>> com.whatsapp然后>>> share_prefs 开放com.whatsapp_preference.xml文件 搜索并选择文件>>>> name = document pdf .... </string>并保存该文件 >>> >>>> apps >>>> whatsapp >>>>并按下停止按钮 再次打开WhatsApp并尝试发送或共享文档

+0

嗨@lomkrodsp我的意思是问我们如何通过我们的应用程序代码做,而不是手动 –

1

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

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

我认为ACTION_SEND意图将意味着“发送到其他应用程序”,而不是striktly“送别处”。

+0

谢谢你回答。让我试着检查它是否有效。看起来可以,但我不确定whatsapp是否允许。 –

+0

是的,如果它不能为你的porblem工作,你也可以给它: sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(exportPath)); –

0

尝试增加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); 
0

共享文本,下面你可以找到一个很好的例子,在那里你可以用具体的数字共享文本,如果你愿意!

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"); 
    } 
} 
2

请注意,如果你的targetSdkVersion为24或更高,我们必须使用FileProvider类给访问特定文件或文件夹,以让其他应用程式存取。

第1步:在应用标签下的AndroidManifest.xml中添加一个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文件。如果文件夹不存在,可能需要创建它。该文件的内容如下所示。它描述了我们希望在名称为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(可选)

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

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

希望这将有助于:)

相关问题