【问题标题】:How to send text file via email as an attachment - Android?如何通过电子邮件发送文本文件作为附件 - Android?
【发布时间】:2021-07-18 05:04:30
【问题描述】:

我在网上看了很多代码,但似乎都遇到了问题。

使用以下函数创建并保存文件:

    private static String filename = "eulerY.txt" ;

    private void saveData() {

      

        FileOutputStream fos_FILE_eulerY = null;

        String message = "hello";
        try {
            fos_FILE_eulerY = openFileOutput(filename , MODE_PRIVATE);
            fos_FILE_eulerY.write(message.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos_FILE_eulerY != null) {
                try {
                    fos_FILE_eulerY.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        // export data
        sendEmail ();
    }

但是,当运行以下代码发送文件时,我一直遇到问题 ClipData.Item.getUri 正如使用此链接“https://stackoverflow.com/questions/48117511/exposed-beyond-app-through-clipdata-item-geturi”的所有答案所建议的那样,打开Gmail时,它说“无法附加文件”

    private void sendEmail (){
        File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
        Uri path = Uri.fromFile(filelocation);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
       // set the type to 'email'
        emailIntent .setType("vnd.android.cursor.dir/email");
        String to[] = {"asd@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
      // the attachment
        emailIntent .putExtra(Intent.EXTRA_STREAM, path);
      // the mail subject
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));
    }


如果有任何方法可以发送此文件,我将不胜感激。

【问题讨论】:

    标签: java android email email-attachments


    【解决方案1】:

    如果您的targetSdkVersion >= 24,那么我们必须使用FileProvider 类来授予对特定文件或文件夹的访问权限,以便其他应用程序可以访问它们。

    第 1 步: 在AndroidManifest.xml 文件中添加以下代码。

            <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    

    第二步

    在res 文件夹内创建一个xml 文件夹。并创建一个名为file_paths.xml 的文件,因为查看&lt;meta-data&gt; 中的上述代码。

    file_paths.xml

    <paths>
        <external-path name="external_files" path="."/>
    </paths>
    

    第三步 现在您可以将文件保存在您的package.private 文件夹中,并且您可以将此文件夹中保存的file uri 共享给其他应用程序,例如gmail 应用程序作为attachment。现在你的方法看起来像:

        private void saveData() {
        String filename = "eulerY.txt" ;
        //FileOutputStream fos_FILE_eulerY = null;
        File externalFilesDirectory = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
        File textFile = new File(externalFilesDirectory,filename);
        String message = "hello";
        try {
            //fos_FILE_eulerY = openFileOutput(textFile.getAbsolutePath() , MODE_PRIVATE);
            //fos_FILE_eulerY.write(message.getBytes());
            FileWriter writer = new FileWriter(textFile);
            writer.append(message);
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.getLocalizedMessage();
        }
    
    
        // export data
        sendEmail (textFile);
    }
    
        private void sendEmail (File file){
        //File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
        //Uri path = Uri.fromFile(filelocation);
        //FileProvider.getUriForFile(it, "${it.packageName}.provider", file)
        Uri fileUri = FileProvider.getUriForFile(this,getPackageName()+".provider",file);
        Intent emailIntent = new Intent(Intent.ACTION_SEND);
        emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        // set the type to 'email'
        emailIntent .setType("vnd.android.cursor.dir/email");
        String[] to = {"asd@gmail.com"};
        emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
        // the attachment
        emailIntent .putExtra(Intent.EXTRA_STREAM, fileUri);
        // the mail subject
        emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
        startActivity(Intent.createChooser(emailIntent , "Send email..."));
    }
    

    【讨论】:

    • 非常感谢。我非常感谢你:)
    猜你喜欢
    • 1970-01-01
    • 2013-07-26
    • 2016-04-18
    • 1970-01-01
    • 2019-10-12
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多