从我的 Android 应用程序中通过 whatsapp 共享 pdf 文件 [英] Share pdf file via whatsapp from my app on Android

查看:101
本文介绍了从我的 Android 应用程序中通过 whatsapp 共享 pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 pdf 文件从我的应用程序发送到 whatsapp,这是代码,但是少了点什么!!

I am try to send pdf file from my app to whatsapp, and here is the code, but something missing!!

它打开whatsapp,我可以选择一个联系人,但它说共享失败"!

it opens whatsapp and i can choose a contact but it says "sharing failed"!

代码

String PLACEHOLDER = "file:///android_asset/QUOT_2016_10(test).pdf";
File f = new File(PLACEHOLDER);
Uri uri = Uri.fromFile(f);

Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_TEXT, "hi");
share.setPackage("com.whatsapp");

share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("application/pdf");

activity.startActivity(share);

推荐答案

我发现了这个问题,如果有人遇到同样的问题,这里是答案.问题是我正在尝试从无效的资产文件夹中打开 pdf,如果尝试从下载文件夹中打开 pdf,它会起作用.最终正确方法请参考以下代码:

I figured out the problem, and here is the answer if somebody had the same issue. The problem was that I am trying to open the pdf from the asset folder which did n't work, and if would try to open the pdf from the download folder for example, it would work. Please refer to the the code below for the final correct way:

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

这篇关于从我的 Android 应用程序中通过 whatsapp 共享 pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆