【问题标题】:Android send file more then 1 MB via Intent to mail applicationAndroid 通过 Intent 向邮件应用程序发送超过 1 MB 的文件
【发布时间】:2015-04-24 22:46:48
【问题描述】:

当我想从 TextView 中获取文本并将其像文本一样放入 Intent 时,我收到“失败的 Binder 事务错误” 对于小文本,它可以正常工作,我可以正确获取文本,并且在我的邮件应用程序中有它之后。

有什么方法可以将文本从我的 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();
    }
}

【问题讨论】:

    标签: android performance email android-intent android-activity


    【解决方案1】:

    我发现了如何实施解决方案 最后打开标准的 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();
                            }
                        }
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      有没有办法将文本从我的 TextView 放入邮件应用程序中,无论大小如何?

      选项包括:

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

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

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

      您将何时以及如何跟踪文件并确保它们已被我删除并且不会向设备发送垃圾邮件。

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

      【讨论】:

      • 感谢您的回答,昨天我实施了解决方案。下面的代码,在我的回答中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 2011-03-12
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多