Android - not able to attach a file in email

前端 未结 7 541
孤城傲影
孤城傲影 2020-12-03 11:48

By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).

I am able to see the fi

7条回答
  •  伪装坚强ぢ
    2020-12-03 12:20

    This Code may help you out to get idea about attachment:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        buttonSend = (Button) findViewById(R.id.buttonSend);
    
        textTo = (EditText) findViewById(R.id.editTextTo);
        textSubject = (EditText) findViewById(R.id.editTextSubject);
        textMessage = (EditText) findViewById(R.id.editTextMessage);
    
        buttonSend.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                String to = textTo.getText().toString();
                String subject = textSubject.getText().toString();
                String message = textMessage.getText().toString();
    
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("plain/text");
                File data = null;
    
                try {
                    Date dateVal = new Date();
                    String filename = dateVal.toString();
                    data = File.createTempFile("Report", ".csv");
                    FileWriter out = (FileWriter) GenerateCsv.generateCsvFile(
                                                    data, "Name,Data1");
                    i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data));
                    i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
                    i.putExtra(Intent.EXTRA_SUBJECT, subject);
                    i.putExtra(Intent.EXTRA_TEXT, message);
                    startActivity(Intent.createChooser(i, "E-mail"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    public class GenerateCsv
    {
        public static FileWriter generateCsvFile(File sFileName,String fileContent) 
        {
            FileWriter writer = null;
    
            try {
                writer = new FileWriter(sFileName);
                writer.append(fileContent);
                writer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            return writer;
        }
    }
    

    The above code requires you add the following permission to your manifest file:

      
    

提交回复
热议问题