如何使用fileprovider将内部存储文件附加到gmail?

时间:2018-07-10 09:00:15

标签: android csv gmail-api android-fileprovider

我需要将一系列文档附加到电子邮件中,代码似乎可以正常工作,但是它从来没有将文件附加到电子邮件中,我一直在寻找Stack Overflow的解决方案,但似乎都指向我使用的方法当前正在使用的页面无法获得正确的功能。

据我了解,以下代码应该可以工作,它不会引发任何错误,但不会附加文件:

private void sendMail(){
    String recipientList = "email@gmail.com";
    String[] recipients = recipientList.split(",");
    String subject = "from app"; //place holder values
    String message = "info from app"; //place holder values

    try{
        // generates the email activity populating the subject, email address and text fields
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL,recipients);
        intent.putExtra(Intent.EXTRA_SUBJECT,subject);
        intent.putExtra(Intent.EXTRA_TEXT,message);

        // the following grants permission from the app to read and write outside of its environment
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        // for loop for each item in directory in order to upload all items to email provider i.e gmail
        String path = getFilesDir().toString();

        File directory = new File(path);
        File[] files = directory.listFiles();

        for(int i = 0; i < files.length;i++){
            // display all files in directory in logcat as a checking process
            Log.d("Files","FileName: "+ files[i].getName() + "\n");
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setPackage("com.google.android.gms");
            Uri fileUri = FileProvider.getUriForFile(this,getString(R.string.file_provider_authority),new File(files[i].getName()));

            // double check that the file is being seen and can be attached
            Log.d("Files","file to be attached: " + fileUri + "\n");
            shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
        }

        // sets chooser to type able to handle appropriate request,
        // could use setType to "text/csv" or "application/csv" etc
        intent.setType("message/rfc822");
        startActivity(Intent.createChooser(intent, "Choose an email client"));
    }catch(Exception e){
        System.out.println("is exception raised during sending mail"+e);
    }
}

Locgcat输出的副本:

  

07-10 04:29:25.760 5843-5843 / com.example.app.serialappv03 D /文件:FileName:340303.csv
  07-10 04:29:25.773 5843-5843 / com.example.app.serialappv03 D /文件:要附加的文件:content://file_provider_authority/root/340303.csv
  07-10 04:29:25.776 5843-5843 / com.example.app.serialappv03 D /文件:FileName:340202.csv
  07-10 04:29:25.782 5843-5843 / com.example.app.serialappv03 D /文件:要附加的文件:content://file_provider_authority/root/340202.csv
  07-10 04:29:25.784 5843-5843 / com.example.app.serialappv03 D /文件:FileName:340902.csv
  07-10 04:29:25.790 5843-5843 / com.example.app.serialappv03 D /文件:要附加的文件:content://file_provider_authority/root/340902.csv
  07-10 04:29:27.847 5843-5873 / com.example.app.serialappv03 D / EGL_emulation:eglMakeCurrent:0xa2f850c0:ver 3 1(tinfo 0xa2f83280)

0 个答案:

没有答案