发送附件并不总是有效的



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

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

我的发送代码-

        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;
        } 

我做错了什么?

问题已解决-我正在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);
    }

所以,如果用户在活动中并发送了文件,那么它就会被发送,但如果用户离开活动并返回并发送,那么它就不会被发送,因为离开活动已经删除了文件。

相关内容

  • 没有找到相关文章

最新更新