的Gmail不会发送电子邮件附件的应用程序推应用程序、发送电子邮件、附件、Gmail

2023-09-07 01:04:18 作者:夕阳沙滩下的携手相伴

在我的Andr​​oid应用程序,我创建了一个,我希望用户能够从我的应用程序通过电子邮件客户端发送PDF。眼下的电子邮件客户端将显示该PDF连接,但无法发送附件。这是一个权限问题还是什么?有什么建议?

修改通过增加存储到外部 getExternalCacheDir()和问题仍然存在。

电子邮件功能

 私人无效EmailDialog(){
        //创建提示用户一个对话框,他们是否要发送的PDF
        dialogBu​​ilder =新AlertDialog.Builder(本);
        最后的TextView txtInput =新的TextView(本);

        dialogBu​​ilder.setTitle(邮件审核);
        dialogBu​​ilder.setMessage(你要通过电子邮件发送本次审计?);
        dialogBu​​ilder.setView(txtInput);
        dialogBu​​ilder.setPositiveButton(添加,新DialogInterface.OnClickListener(){
            //允许用户发送PDF在他们的电子邮件应用程序的选择
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                意图I =新的意图(Intent.ACTION_SEND);
                i.setType(信息/ RFC822);
                PDF =新的文件(getExternalCacheDir()+ PDFname);
                i.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(PDF));
                尝试 {
                    startActivity(Intent.createChooser(我,发送邮件...));
                }赶上(android.content.ActivityNotFoundException前){
                    Toast.makeText(OldLocation.this,没有安装任何邮件客户端,Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialogBu​​ilder.setNegativeButton(删除,新DialogInterface.OnClickListener(){
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释它){
                RemoveDialog();
            }
        });

        AlertDialog emailDialog = dialogBu​​ilder.create();
        emailDialog.show();
    }
 

PDF功能

  @TargetApi(Build.VERSION_ codeS.KITKAT)
    私人无效createPDF(){


        //打开一个新的文档
        PrintAttributes PA =新PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.NA_LETTER).setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
        PrintedPdfDocument文档=新PrintedPdfDocument(这一点,PA);

        //启动页面
        PdfDocument.Page页= document.startPage(0);

        //画的东西在页面上
        查看内容= TextView的;
        content.draw(page.getCanvas());



        //完成页面
        document.finishPage(页);

        //添加更多的页面

        //写文档内容

        尝试 {
            文件的PDF = getExternalCacheDir();
            档案文件=新的文件(PDF文件,companyInfo.getName()+.PDF);
            file.createNewFile();
            FileOutputStream中FOS =新的FileOutputStream(文件);
            document.writeTo(FOS);
            fos.close();

            //关闭文档
            document.close();
        }赶上(例外前){

        }

        realm.beginTransaction();
        公司信息= realm.where(CompanyInfo.class).findFirst();
        companyInfo.removeFromRealm();
        realm.commitTransaction();
    }
 
电邮应用程序为了Gmail邮件app下载 电邮应用程序为了Gmail邮件手机版下载 手机电邮应用程序为了Gmail邮件下载

解决方案   

这是一个权限问题还是什么?

是的。有精确为零的应用程序在这个星球的面貌,比你其他的,可以访问您的文件,因为它是对的内部存储,专用于您的应用程序。

此外,永远难以code PATHS 。你的 /数据/数据​​/ 的东西会失败每次的Andr​​oid 4.2+设备,用户是在辅助账户上。

  

有什么建议?

您可以使用内部存储棒,但你需要使用的在 FileProvider ,也许的我的 LegacyCompatCursorWrapper ,服务文件。 此示例应用说明了这一点,尽管 ACTION_VIEW 而不是 ACTION_SEND

或者,穿上外部存储文件

In my android app I have created a PDF that I would like the user to be able to send from my app through an email client. Right now the email client will show that the PDF is attached but is unable to send the attachment. Is this a permissions problem or something? Any suggestions?

EDIT stored to external by adding getExternalCacheDir() and problem persists

Email function

private void EmailDialog(){
        //create a dialog that prompts the user whether or not they want to send the PDF
        dialogBuilder = new AlertDialog.Builder(this);
        final TextView txtInput = new TextView(this);

        dialogBuilder.setTitle("Email Audit");
        dialogBuilder.setMessage("Do you want to email this audit?");
        dialogBuilder.setView(txtInput);
        dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
            //allows user to send PDF over their email application choice
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                PDF = new File(getExternalCacheDir() + PDFname);
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(PDF));
                try {
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(OldLocation.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                RemoveDialog();
            }
        });

        AlertDialog emailDialog = dialogBuilder.create();
        emailDialog.show();
    }

PDF function

@TargetApi(Build.VERSION_CODES.KITKAT)
    private void createPDF(){


        // open a new document
        PrintAttributes pa = new PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.NA_LETTER).setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();
        PrintedPdfDocument document = new PrintedPdfDocument(this, pa);

        // start a page
        PdfDocument.Page page = document.startPage(0);

        // draw something on the page
        View content = textView;
        content.draw(page.getCanvas());



        // finish the page
        document.finishPage(page);

        // add more pages

        // write the document content

        try {
            File PDFs = getExternalCacheDir();
            File file = new File(PDFs, companyInfo.getName() + ".pdf");
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            document.writeTo(fos);
            fos.close();

            //close the document
            document.close();
        } catch (Exception ex){

        }

        realm.beginTransaction();
        companyInfo = realm.where(CompanyInfo.class).findFirst();
        companyInfo.removeFromRealm();
        realm.commitTransaction();
    }

解决方案

Is this a permissions problem or something?

Yes. There are exactly zero apps on the face of the planet, other than yours, that can access your file, as it is on internal storage, private to your app.

Also, NEVER HARDCODE PATHS. Your /data/data/ stuff will fail on every Android 4.2+ device where the user is on a secondary account.

Any suggestions?

You can stick with internal storage, though you will need to use a FileProvider, perhaps with my LegacyCompatCursorWrapper, to serve the file. This sample app demonstrates this, albeit with ACTION_VIEW instead of ACTION_SEND.

Or, put the file on external storage.

 
精彩推荐
图片推荐