2016-09-16 8 views
3

次のコードを試しましたが、pdfファイルが添付されていません。アンドロイドの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-pdfthrough-whatsapp-to-an-unsaved-contact –

-2

は、データ>>>データ>>> com.whatsapp、その後>>> share_prefs オープンcom.whatsapp_preference.xmlファイルを>>>に行くアンドロイド にマネージャーのアプリを提出し、 それを開くために行きます >>>> name = document pdf .... </string>を検索してファイルを選択し、このファイルを保存します。 >>>>設定>>>> apps >>>> 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

はいそれがあなたの問題を解決しない場合は、それを与えることもできます: 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ファイルを作成します。フォルダが存在しない場合は作成するためにフォルダが必要な場合があります。ファイルの内容を以下に示します。外部フォルダへのアクセスをルートフォルダ(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 uri = FileProvider.getUriForFile(PdfRendererActivity.this, PdfRendererActivity.this.getPackageName() + ".provider", outputFile); 

ステップ4(オプション)

Uri photoURI = Uri.fromFile(outputFile); 

に以下のコードの行を変更することである。

tを作成する意図を使用している場合彼はあなたが次のコード行を追加する必要があり、あなたのファイルをシステムを開きます

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

希望、これは私たちがコードで我々のアプリを通してそれを行うことができますどのように依頼することを意味:)

関連する問題