java—访问外部存储文件并将其作为电子邮件附件发送

qmb5sa22  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(190)

我想发一封附有pdf文件的电子邮件。当我试图通过uri发送它们时,会收到以下错误消息:

android.os.FileUriExposedException: file:///Download/myPDFFile.pdf exposed beyond app through ClipData.Item.getUri()

所以我试着对文件提供者做同样的事情。在这里我得到这个错误信息:

java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority com.example.digitalpaper.provider
String filename="myPDFFile.pdf";

                File testFile = new File(Environment.DIRECTORY_DOWNLOADS, filename);

                Uri path =    FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),BuildConfig.APPLICATION_ID + ".provider", testFile);

               //----------------Sending the Mail----------------//
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("application/pdf");
                String to[] = {"xyz@gmx.de",eT_email.getText().toString()};
                emailIntent.putExtra(Intent.EXTRA_EMAIL, to);

                emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject");

//-----------------------Attaching the file---------------------------------//

                emailIntent.putExtra(Intent.EXTRA_STREAM, path);
              //  emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));

                startActivity(Intent.createChooser(emailIntent,"Send email..."));

我已经尝试了几种方法来完成电子邮件附件,但我永远无法访问我的外部存储。有人知道怎么解决吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题