android 工作室通过电子邮件 Outlook 发送了 pdf

android studio sent pdf via email Outlook

我有一个大问题。我希望你能帮助我。 当我通过 outlook 发送 pdf 时,出现错误 -> 无法添加 Attach

当我通过 Samsung-Email 发送 pdf 时,它有效。 但我需要通过 Outlook 发送 pdf。

清单:

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

file_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="external" path="." />
    <external-files-path name="my_images" path="." />
    <external-files-path name="external_files" path="." />
    <external-files-path name="cache" path="." />
    <external-files-path name="files" path="." />
    <external-files-path name="root" path="." />
</paths>

我的代码:

 private String Speichern_Path="/ABO/Dokumente/Reparaturbericht/";

            String emailText ="";
            String emailBetreff="";

            //String directory_path=Environment.getExternalStorageDirectory()+Speichern_Path+Pdfname;

            emailBetreff ="Reparaturbericht";
            emailText = "Hallo";

            Toasty.info(context,"Email wird geöffnet",Toast.LENGTH_SHORT).show();

            Intent i = new Intent(Intent.ACTION_SEND);
            //i.setType("*/*");
            i.setType("application/pdf");

            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{Reparatur_Email});
            i.putExtra(Intent.EXTRA_SUBJECT, emailBetreff);
            i.putExtra(Intent.EXTRA_TEXT   , emailText);


            File root = Environment.getExternalStorageDirectory();
            String pathToMyAttachedFile = Speichern_Path+ Pdfname;
            File file = new File(root, pathToMyAttachedFile );
            if (!file.exists() || !file.canRead()) {
                return;
            }
           Uri uri = Uri.fromFile(file);
           //Uri uri = FileProvider.getUriForFile(context, "com.example.android.fileprovider",file);
            i.putExtra(Intent.EXTRA_STREAM, uri);



            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            try {
               
                startActivity(Intent.createChooser(i, "Email senden"));
                
            }
            catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
            }


我修改了代码,但还是不行

   String emailText ="";
            String emailBetreff="";

            String directory_path = Environment.getExternalStorageDirectory() + Speichern_Path+Pdfname;

            emailBetreff ="Reparaturbericht";
            emailText = "Hallo,"
                    +"\n\nanbei schicke ich euch den Reparaturbericht."
                    +"\n\nViele Grüße\n"
                    +Reparatur_User
                    +"\n\nDiese Email wurde automatisch erstellt.\n\n"
                    +App_name;

            Toasty.info(context,"Email wird geöffnet",Toast.LENGTH_SHORT).show();

            Intent i = new Intent(Intent.ACTION_SEND);
            //i.setType("*/*");
            i.setType("application/pdf");
           

            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{Reparatur_Email});
            i.putExtra(Intent.EXTRA_SUBJECT, emailBetreff);
            i.putExtra(Intent.EXTRA_TEXT   , emailText);


            File root = Environment.getExternalStorageDirectory();
            String pathToMyAttachedFile = Speichern_Path+ Pdfname;
            File file = new File(root, pathToMyAttachedFile);
            if (!file.exists() || !file.canRead()) {
                return;
            }
            //Uri uri = Uri.fromFile(file);
           Uri uri = FileProvider.getUriForFile(context, "com.example.android.fileprovider",file);
            i.putExtra(Intent.EXTRA_STREAM, uri);



            i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            try {

                startActivity(Intent.createChooser(i, "Email senden"));

            }
            catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
            }

当我通过 Samsung-Email 发送 pdf 时,它有效。但我需要通过 Outlook 发送 pdf。

我找到了使用 Outlook 发送电子邮件的答案:

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("*/*");

i.putExtra(Intent.EXTRA_EMAIL, new String[]{Reparatur_Email});
i.putExtra(Intent.EXTRA_SUBJECT, emailBetreff);
i.putExtra(Intent.EXTRA_TEXT, emailText);

File root = Environment.getExternalStorageDirectory();
String pathToMyAttachedFile = Speichern_Path + Pdfname;
File file = new File(root, pathToMyAttachedFile);

if (!file.exists() || !file.canRead()) {
    return;
}

//Uri uri = Uri.fromFile(file);
Uri uri = FileProvider.getUriForFile(context, "com.example.android.fileprovider",file);
i.putExtra(Intent.EXTRA_STREAM, uri);

context.grantUriPermission(getActivity().getPackageName(),uri,Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
context.revokeUriPermission(uri,Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

List<ResolveInfo> resolveInfos_List = context.getPackageManager().queryIntentActivities(i,PackageManager.MATCH_DEFAULT_ONLY);

for (ResolveInfo resolveInfo:resolveInfos_List){
    String Package_Name = resolveInfo.activityInfo.packageName;
 
    context.grantUriPermission(Package_Name,uri,Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}

try {       
    startActivity(Intent.createChooser(i, "Email senden"));
}
catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
}

File_Path:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external" path="." />
    <external-files-path name="my_images" path="." />
    <external-files-path name="external_files" path="." />
    <cache-path name="cache" path="." />
    <external-cache-path
        name="external-cache"
        path="." />
    <external-files-path name="root" path="." />
    <files-path name="files" path="." />
</paths>