2016-09-16 412 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); 

希望這將有助於:)

相關問題