Android 通过 Intent to mail 应用程序发送超过 1 MB 的文件

Android send file more then 1 MB via Intent to mail application

当我想从 TextView 中获取文本并将其像 Intent 中的文本一样放置时,我得到 "Failed Binder Transaction Error" 对于小文本,它可以工作,我可以正确地获取文本,并且在我的邮件应用程序中获得它之后。

有没有什么方法可以将我的 TextView 中的任何大小的文本放入邮件应用程序中? 您将何时以及如何跟踪文件并确保它们已被删除并且不会向设备发送垃圾邮件。那解决方案的设计是什么?

下面是我拥有的适用于少量文本的代码..

private void sendLogMail() {
        try {
            if ("".equals(logTextView.getText().toString().trim())) {
                Toast.makeText(getApplicationContext(), "Sorry, no log information to send", Toast.LENGTH_LONG).show();
            }
            else {
                String[] mailReceiver = new String[] { getString(R.string.logviewer_mailto)};
                startActivity( Intent.createChooser(
                        new Intent(Intent.ACTION_SEND).setData(Uri.parse("mail:to")).setType("text/plain")
                                .putExtra(Intent.EXTRA_EMAIL, mailReceiver)
                                .putExtra(Intent.EXTRA_SUBJECT, "Log:  " + Utils.formatReadableTimestamp( System.currentTimeMillis()))
                                .putExtra(Intent.EXTRA_TEXT, getInfo() + "\n" + "\n" + "Log: "+ "\n"+logTextView.getText()),
                                 "Please config your  mail account") );
            }
        }
        catch (Exception e) {
            getLog().error("Error sending logfile as email", e);
        }
    }

    private String getInfo() throws Exception {
        return new StringBuilder()
                .append("Terminal ID: " + getCore().getID())
                .append("\n")
                .append("License ID: " + getCore().getLicense())
                .append("\n")
                .append("Outlet ID: " + getCore().getOtherID())
                .append("\n")
                .append("Name: "+" "+getLoggedIn().getFormattedName())
                .append("/")
                .append(" ")
                .append(getLoggedIn().getId())
                .toString();
    }
}

Is there any way to put text whatever size it has from my TextView into Mail application?

选项包括:

  • 将其写入内部存储上的文件并使用 FileProvider 将其提供给邮件客户端,将 Uri 放入 EXTRA_STREAM

  • 创建一些 ContentProvider 以将其从 RAM 提供给邮件客户端,将 Uri 放入 EXTRA_STREAM

  • 将其写入外部存储上的文件以将其提供给邮件客户端,将Uri放入EXTRA_STREAM

    [=35中的文件=]

When and how would you keep track of the files and make sure they I'm deleted and not spamming the device.

我会将文件保存在内部存储器中,也许在 getCacheDir() 中,然后在 24 小时或类似时间后将其删除。

我找到了如何实施解决方案 最后打开标准 Android ICS 预装的邮件应用程序以及邮件的附件。

希望这对其他人也有帮助

代码如下

一般方法

 private void sendLogMail() {
            try {
                if ("".equals(logTextView.getText().toString().trim())) {
                    Toast.makeText(getApplicationContext(), "Sorry, no log information to send", Toast.LENGTH_LONG).show();
                }
                else {
                    String[] mailReceiver = new String[] { getString(R.string.logviewer_mailto)};
                    Intent emailIntent = new Intent(Intent.ACTION_SEND);

                    StringBuilder logBuffer = new StringBuilder();
                    logBuffer.append( getTerminalInfo() ).append( "\n\n" );
                    logBuffer.append( "Log attached!" );

                    File logFile = writeLogFile( getInfo() );
                    Uri uri = Uri.fromFile( logFile );

                    emailIntent.setData(Uri.parse("mail:to"));
                    emailIntent.putExtra(Intent.EXTRA_EMAIL, mailReceiver);
                    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Log:  " + Utils.formatReadableTime( System.currentTimeMillis()));
                    emailIntent.putExtra(Intent.EXTRA_TEXT, logBuffer.toString() );

                    emailIntent.setType("text/plain");
                    if (uri != null) {
                        emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                    }

                    startActivity( Intent.createChooser( emailIntent, "Please choose the mail client to use" ) );

                }
            }

这里我只是简单的写入文件

private File writeLogFile( String terminalInfo ) throws IOException {
        File dir = Logger.getInstance().getPublicLogDirectory();

        File logFile = new File( dir.getAbsolutePath(),+Utils.getFormattedTime( System.currentTimeMillis()) + ".log" );
        OutputStream os = new BufferedOutputStream( new FileOutputStream( logFile ) );

        try {
            os.write( terminalInfo.getBytes() );
            os.write( "\n\n".getBytes() );
            os.write( logTextView.getText().toString().getBytes() );
            os.flush();
        }
        finally {
            os.close();
        }

        return logFile;
    }

private String getInfo() throws Exception {
        return new StringBuilder()
                .append("Terminal ID: " + getCore().getID())
                .append("\n")
                .append("License ID: " + getCore().getLicense())
                .append("\n")
                .append("Outlet ID: " + getCore().getOtherID())
                .append("\n")
                .append("Name: "+" "+getLoggedIn().getFormattedName())
                .append("/")
                .append(" ")
                .append(getLoggedIn().getId())
                .toString();
    }

Make directory



     public File getPublicLogDirectory() {
                File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                dir.mkdirs();

                File logDir = new File( dir, "logs" );
                logDir.mkdirs();

                return logDir;
            }

    check if files exist
        public void cleanPublicLogDirectory() throws IOException {
            File dir = getPublicLogDirectory();

            File logFiles[] = dir.listFiles();

            if( logFiles != null && logFiles.length > 0 ) {
                for( File logFile : logFiles ) {
                    if( logFile.getName().endsWith( ".log" ) ) {
                        long lastModified = logFile.lastModified();

                        // Delete files older than 24 hours
                        if( (System.currentTimeMillis() - lastModified) > 1 ) { // 1000*60*60*24 ) { 
                            logFile.delete();
                        }
                    }
                }
            }
        }