发送附件并不总是有效

时间:2014-10-31 14:23:33

标签: android email-attachments

有时我会看到没有附件的电子邮件。

我怀疑当文件大小比处理程序大时会发生这种情况。

我的发送代码 -

        Intent email = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);

        email.putExtra(Intent.EXTRA_SUBJECT, "Device Logs");

        email.setType("text/plain");

        email.putExtra(Intent.EXTRA_TEXT, getEmailContents());

        File file = writeLogFile();

        ArrayList<Uri> uriList = new ArrayList<Uri>();

        Uri uri = Uri.fromFile(file);

        uriList.add(uri);

        email.putExtra(Intent.EXTRA_STREAM, uriList);

        startActivity(Intent.createChooser(email, "Choose an Email client :"));

文件代码 -

public File writeLogFile()
        {

            if(builder != null)
            {
                for(int i =0;i <receiptlist.size();i++)
                {
                    builder.append(receiptlist.get(i) + "\n");
                }
            }

            //to create a Text file name "logcat.txt" in SDCard  
            File sdCard = Environment.getExternalStorageDirectory();  
            File dir = new File (sdCard.getAbsolutePath() + "/myLogcat");  

            if(!dir.exists())
                dir.mkdirs();  

            File file = new File(dir, "logcat.txt");

            try {    
                  //to write logcat in text file  
                  FileOutputStream fOut = new FileOutputStream(file);  
                  OutputStreamWriter osw = new OutputStreamWriter(fOut);   

                    // Write the string to the file  
                    osw.write(builder.toString());              
                    osw.flush();  
                    osw.close();  
                 } catch (FileNotFoundException e) {  
                  e.printStackTrace();  
                 } catch (IOException e) {  
                  e.printStackTrace();  
                 }

                return file;
        } 

我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题已解决 - 我正在onDestroy()删除该文件。

@Override
    public void onDestroy()
    {
        super.onDestroy();  
        File sdCard = Environment.getExternalStorageDirectory();  
        String  sdPath = sdCard.getAbsolutePath() + "/myLogcat";  

        File sdDir = new File(sdPath);

        if(sdDir.exists())
            deleteRecursive(sdDir);
    }

因此,如果用户处于活动状态并发送该文件,则会发送该文件,但如果用户离开活动并返回并发送,则不会发送,因为该活动已经删除了该文件。