无法附加文件发送电子邮件的android 11 [英] couldn't attach file send email android 11

查看:124
本文介绍了无法附加文件发送电子邮件的android 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个通过电子邮件发送处理报告的应用程序.在应用程序中完成处理数据后,用户将发送一封包含处理数据的电子邮件.但是,我在Android 11上遇到问题.将报告附加到电子邮件时,无论使用哪种电子邮件应用程序,都会出现一条消息:无法附加文件.我一直在研究,发现它可能与在android 11版本的设备上访问设备内部存储的权限有关.我希望我能帮助您发送带有android 11附件文件的电子邮件./p>

我的代码:清单

 < uses-permission android:name =" android.permission.ACCESS_NETWORK_STATE"/>< uses-permission android:name =" android.permission.INTERNET"/>< uses-permission android:name =" android.permission.READ_EXTERNAL_STORAGE"/>< uses-permission android:name =" android.permission.WRITE_EXTERNAL_STORAGE"/><应用android:allowBackup =" true"android:icon =&"; @ mipmap/ic_launcher;android:label =" @ string/app_name"android:networkSecurityConfig =" @ xml/network_security_config"android:roundIcon =" @ mipmap/ic_launcher_round"android:supportsRtl =" true"android:theme ="@ style/AppTheme"android:requestLegacyExternalStorage ="true"工具:忽略="AllowBackup";工具:targetApi ="n"<活动.....<提供者android:name =" androidx.core.content.FileProvider"android:authorities =" $ {applicationId} .provider"android:exported =" false"android:grantUriPermissions =" true"><元数据android:name ="android.support.FILE_PROVIDER_PATHS"android:resource =" @ xml/file_paths"/></provider></应用程序> 

我的xml:

 <?xml version ="1.0"encoding ="utf-8"?<路径>< external-path name ="external_files"path ="."</paths> 

保存文件.pdf

  val mDocProc = Document()val文件=文件(Environment.getExternalStorageDirectory().toString()+"/"+Relatóriodiário";+日期+".pdf")如果(file.exists()){file.delete()}//pdf文件路径mFilePathDaily =Environment.getExternalStorageDirectory().toString()+"/"+"Relatóriodiário"+日期+".pdf" 

班级发送电子邮件:

  val报告= File(mFilePathDaily)val uri = FileProvider.getUriForFile(activity.requireContext(),activity.requireContext().applicationContext.packageName +.provider",报告)val i = Intent(Intent.ACTION_SENDTO)i.data = Uri.parse("mailto:")i.putExtra(Intent.EXTRA_EMAIL,arrayOf("testesdissertacao@gmail.com"))i.putExtra(Intent.EXTRA_SUBJECT,EMAIL_SUBJECT)i.putExtra(Intent.EXTRA_TEXT,基准测试")i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)i.putExtra(Intent.EXTRA_STREAM,uri)//i.type ="image/png"activity.requireContext().startActivity(Intent.createChooser(i,null))progressDialog.dismiss() 

现在的问题是,您找不到要附加到电子邮件的报告.我注意到该报告保存在模拟器的另一个文件夹中,而不是根文件夹中.我认为这就是为什么您找不到要附加到电子邮件的文件的原因.

请勿将文件附加到电子邮件中

解决方案

我已成功将图像附加到电子邮件中,我会给您一个代码

将您的URI转换为文件

  private void共享(文件保存路径){如果(savePath!= null){Uri uri = FileProvider.getUriForFile(context,context.getApplicationContext().getPackageName()+".provider",savePath);意图i =新意图(Intent.ACTION_SEND);i.putExtra(Intent.EXTRA_EMAIL,新的String [] {"fake@fake.edu"});i.putExtra(Intent.EXTRA_SUBJECT,在工作中");//Log.d("URI @!@#!#!@ ## !!,Uri.fromFile(pic).toString()+"" pic.exists());i.putExtra(Intent.EXTRA_TEXT,电子邮件的所有详细信息都在消息中");i.putExtra(Intent.EXTRA_STREAM,uri);i.setType("image/png");context.startActivity(Intent.createChooser(i,在工作中分享你")));} 

如何在XML路径中添加提供程序首先,您必须创建一个XML资源文件夹,然后创建一个XML资源文件.粘贴此代码后

 <路径xmlns:android ="http://schemas.android.com/apk/res/android">< external-path name ="external_files"path ="."</paths> 

然后还添加了清单

 <提供者android:name =" androidx.core.content.FileProvider"android:authorities =" $ {applicationId} .provider"android:exported =" false"android:grantUriPermissions =" true"><元数据android:name ="android.support.FILE_PROVIDER_PATHS"android:resource =" @ xml/provider_paths"/></provider> 

I am developing an application that sends processing reports via email. When completing the processing data in the application, the user sends an email containing the processing data. However, I am facing a problem on Android 11. When attaching the report to the email, regardless of the email application, a message appears: the file could not be attached. I've been researching a little and saw that it may be related to the permission to access the internal storage of the device on devices with version of android 11. I would like me to help you send email with file attached on android 11.

My code: Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:networkSecurityConfig="@xml/network_security_config"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:requestLegacyExternalStorage="true"
    tools:ignore="AllowBackup"
    tools:targetApi="n">

    <activity.....
     <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>
  </application>

My xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
   <external-path name="external_files" path="."/>
</paths>

Save file .pdf

val mDocProc = Document()
            val file =
                File(
                    Environment.getExternalStorageDirectory()
                        .toString() + "/" + "Relatório diário" + date + ".pdf"
                )
            if (file.exists()) {
                file.delete()
            }
            //pdf file path
            mFilePathDaily =
                Environment.getExternalStorageDirectory()
                    .toString() + "/" + "Relatório diário " + date + ".pdf"

Class send email:

val report = File(mFilePathDaily)
            val uri = FileProvider.getUriForFile(
                activity.requireContext(),
                activity.requireContext().applicationContext.packageName + ".provider",
                report
            )

            val i = Intent(Intent.ACTION_SENDTO)
            i.data = Uri.parse("mailto:")
            i.putExtra(Intent.EXTRA_EMAIL, arrayOf("testesdissertacao@gmail.com"))
            i.putExtra(Intent.EXTRA_SUBJECT, EMAIL_SUBJECT)
            i.putExtra(Intent.EXTRA_TEXT, "Segue em anexo o relatório de Benchmark")
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            i.putExtra(Intent.EXTRA_STREAM, uri)
            //i.type = "image/png"
            activity.requireContext().startActivity(Intent.createChooser(i, null))
            progressDialog.dismiss()

Now the problem is that you can't find the report to attach to the email. I noticed that the report was saved inside that other folder in the emulator, and not in the root folder. I think that is why you are not finding the file to attach to the email.

Do not attach the file to the email

解决方案

I have attached the image successfully in email i will give you a code

convert your URI to file

 private void Share(File savepath) {
       
 if (savePath != null) {

            Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", savePath);

            Intent i = new Intent(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@fake.edu"});
            i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
            //Log.d("URI@!@#!#!@##!", Uri.fromFile(pic).toString() + "   " + pic.exists());
            i.putExtra(Intent.EXTRA_TEXT,"All Detail of Email are here in message");
            i.putExtra(Intent.EXTRA_STREAM,uri);
            i.setType("image/png");
            context.startActivity(Intent.createChooser(i,"Share you on the jobing"));

        }

how to add provider in XML path first, you have to create an XML resource folder then create an XML resource file after pasting this code

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

then also add-in manifest

<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/provider_paths" />
</provider>

这篇关于无法附加文件发送电子邮件的android 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆