java - 在默认电子邮件客户端上发送 pdf - Android 应用程序

标签 java android email-attachments

我需要在消息中发送 PDF 文件附件,我有一个按钮可以调用一个函数来打开一个包含消息、电子邮件地址和主题的 Intent,但我还需要附加 PDF 文件。

这是我的代码,我找不到我的错误,有人可以帮助我吗?

 public void initializeWebView() {
        // Initialize the webview

        webView.setResourceClient(new XWalkResourceClient(webView) {
            @Override
            public boolean shouldOverrideUrlLoading(XWalkView view, String stringUrl) {

                if(stringUrl.equals(baseUrl)) {
                    return false;
                }

                // mailto links will be handled by the OS.
                if (stringUrl.startsWith("mailto:")) {
                    Uri uri = Uri.parse(stringUrl);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    String fileName = "bouhnik.pdf";
                    String filePath = (Configuration.getMagazineAssetPath()).toString()+ File.separator  + fileName;
                    Context c = getActivity().getApplicationContext();
                    File file = null;
                    FileOutputStream fos = null;
                    try {

                        InputStream is = c.getAssets().open(filePath);

                        int size = is.available();
                        byte[] buffer = new byte[size];
                        is.read(buffer);
                        is.close();


                        fos = new FileOutputStream(file);
                        fos.write(buffer);
                        fos.close();


                    } catch (IOException e) {
                        Log.i("Ferrou",e.toString());
                        e.printStackTrace();
                    }
                    if (!file.exists() || !file.canRead()) {
                        return false;
                    }
                    intent.putExtra(intent.EXTRA_STREAM, file.getPath());
                    intent.setClassName("com.android.email", "com.android.mail.compose.ComposeActivity");
                    intent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
                    WebViewFragment.this.startActivity(Intent.createChooser(intent, "Send email..."));

                } else {
                    try {
                        URL url = new URL(stringUrl);


                        // We try to remove the referrer string to avoid passing it to the server in case the URL is an external link.
                        String referrer = "";
                        if (url.getQuery() != null) {
                            Map<String, String> variables = Configuration.splitUrlQueryString(url);
                            String finalQueryString = "";
                            for (Map.Entry<String, String> entry : variables.entrySet()) {
                                if (entry.getKey().equals("referrer")) {
                                    referrer = entry.getValue();
                                } else {
                                    finalQueryString += entry.getKey() + "=" + entry.getValue() + "&";
                                }
                            }
                            if (!finalQueryString.isEmpty()) {
                                finalQueryString = "?" + finalQueryString.substring(0, finalQueryString.length() - 1);
                            }
                            stringUrl = stringUrl.replace("?" + url.getQuery(), finalQueryString);
                        }
                        // Remove referrer from query string
                        if (!url.getProtocol().equals("file")) {
                            if (referrer.equals(WebViewFragment.this.getActivity().getString(R.string.url_external_referrer))) {
                                Uri uri = Uri.parse(stringUrl);
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                WebViewFragment.this.startActivity(intent);
                            } else if (referrer.toLowerCase().equals(WebViewFragment.this.getActivity().getString(R.string.url_baker_referrer))) {
                                ((IssueActivity) WebViewFragment.this.getActivity()).openLinkInModal(stringUrl);
                                return true;
                            } else {
                                return false;
                            }
                        } else {
                            stringUrl = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
                            int index = ((IssueActivity) WebViewFragment.this.getActivity()).getJsonBook().getContents().indexOf(stringUrl);
                            if (index != -1) {
                                Log.d(this.getClass().toString(), "Index to load: " + index + ", page: " + stringUrl);
                                ((IssueActivity) WebViewFragment.this.getActivity()).getViewPager().setCurrentItem(index);
                                view.setVisibility(View.GONE);
                            } else {
                                // If the file DOES NOT exist, we won't load it.
                                File htmlFile = new File(url.getPath());
                                if (htmlFile.exists()) {
                                    return false;
                                }
                            }
                        }
                    } catch (MalformedURLException | UnsupportedEncodingException ex) {
                        Log.d(">>>URL_DATA", ex.getMessage());
                    }
                }

                return true;
            }
        });

        // Set UI Client (Start stop animations)
        webView.setUIClient(new XWalkUIClient(webView) {

            @Override
            public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
                if(!url.isEmpty() && status == LoadStatus.FINISHED) {
                    if(isUserVisible) {
                        webView.resumeTimers();
                    }else{
                        webView.pauseTimers();
                    }

                }
            }
        });
        webView.load(baseUrl, null);
    } 

最佳答案

非常感谢大家!!

我解决了我的问题,将 Intent 类型更改为:

Intent emailIntent = new Intent(Intent.ACTION_SEND);

因为这更好地发送电子邮件命令,并且我定义了一个 emailUri,其中:

emailUri = Uri.fromFile(file.getAbsoluteFile());

因为这会得到一个包含文件的绝对路径,当电子邮件客户端打开时,它会打开这个文件,而不是路径。

我按照自己的 Intent 添加类型,但我选择了附件的类型,因此我定义:

 emailIntent.setType("application/pdf");

最后:

emailIntent.putExtra(Intent.EXTRA_STREAM, uriMail);
 startActivity(emailIntent);

现在可以用了!!谢谢:D

关于java - 在默认电子邮件客户端上发送 pdf - Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31518139/

相关文章:

java - 从 ArrayList 获取特定类型的第一个元素

android - Gradle 项目同步失败

android - 电子邮件附件的 Intent 过滤器

java - 使用 JDK5 进行跟踪

java - 两个不同类之间的类型转换

java - 我可以在 Android 上使用 PixelMed 吗?

android - 延迟暂停、早期恢复和唤醒锁。请解释

java - 如何在 TextView Android 上使用带有文本的图像(设备的存储图像)?

php - Zend 框架 : How to read email attachments (and save to disk)?

asp-classic - ASP 发送带附件的电子邮件表单