Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法附加文件;创建和发送带有活动意图的PDF_Java_Android - Fatal编程技术网

Java 无法附加文件;创建和发送带有活动意图的PDF

Java 无法附加文件;创建和发送带有活动意图的PDF,java,android,Java,Android,我以前也问过类似的问题,不过,我做了更多的研究,我仍在努力找出我的问题所在。从本质上说,我试图做的是在客户完成购买后,以PDF格式创建收据。新创建的PDF需要通过Intent发送到单独的电子邮件客户端(如Gmail)。与我前面的问题相比,我现在实现了一个文件提供程序,但是我不断得到两个不同的错误。第一个是: java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri

我以前也问过类似的问题,不过,我做了更多的研究,我仍在努力找出我的问题所在。从本质上说,我试图做的是在客户完成购买后,以
PDF
格式创建收据。新创建的
PDF
需要通过
Intent
发送到单独的电子邮件客户端(如Gmail)。与我前面的问题相比,我现在实现了一个
文件提供程序
,但是我不断得到两个不同的错误。第一个是:

java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content...requires the provider be exported, or grantUriPermission()
这会导致文件根本不会附加到电子邮件。我有时会遇到的第二个错误是,它会将实际的URI路径放入电子邮件的收件人部分,您可以在下面看到(也会导致文件未附加)

在做研究时,我尝试了很多不同的解决方案,例如使用
setFlags(Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION)
addFlags(Intent.FLAG\u GRANT\u READ\u URI\u PERMISSION)
,可以看到

我还看到一些人说在清单中使用了
android:requestLegacyExternalStorage=“true”
,然而,这将极大地限制我的最低APK级别

我甚至尝试了这一建议,即手动检查并授予所有包的权限,这仍然没有解决我的问题

我还尝试了使用
grantUriPermission()
方法,但仍然不起作用

下面,您将看到我放置了一些代码:

Manifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fabricanddecor">

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

    <application
        android:allowBackup="true"
        android:largeHeap="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".LoginActivity"
            android:configChanges="orientation|screenSize"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan">
        </activity>
        <provider
            android:authorities="com.example.fabricanddecor"
            android:name="androidx.core.content.FileProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>

    </application>

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

我们将非常感谢您的任何帮助或建议,并感谢您抽出时间。如果需要任何进一步的代码,我很乐意效劳。

在您的清单中,您使用:android:exported=“false”定义提供者您是否尝试将其设置为true?考虑到这就是您编写的错误所需要的
catch(IOException ioe){Log.e(“文件错误”,“错误”+ioe.toString());}
在这样一个异常之后,您可以执行该异常,因为它不在那里,然后继续执行。当然,你应该停下来。显示一个祝酒词,让用户知道。您正在创建一个pdf文件,然后共享它。这是两件事。如果您可以创建一个pdf文件,那么您不需要发布该代码。请只发布有问题的代码。我认为只有共享才有问题。
file.mkdirs()仅在目录尚不存在时调用mkdirs。并检查返回值。如果返回false,则停止/返回并显示一个toast。
String fileDirectory=directory+“sale.pdf
?directory?最好将其命名为:
String filePath=directory+“sale.pdf”
try {
            String directory = Environment.getExternalStorageDirectory().getPath();
            File file = new File(directory);
            if (file.mkdirs()) {
                String filePath = directory + "/sale.pdf";
                File file2 = new File(filePath);
                Uri contentUri = FileProvider.getUriForFile(getContext(), "com.example.fabricanddecor", file2);
                Intent intent = new Intent(Intent.ACTION_SEND);

                getContext().grantUriPermission("com.example.fabricanddecor", contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

                intent.putExtra(Intent.EXTRA_EMAIL, recipient);
                intent.putExtra(Intent.EXTRA_SUBJECT, subject);
                intent.putExtra(Intent.EXTRA_STREAM, contentUri);
                intent.setDataAndType(contentUri, "application/pdf");
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivity(Intent.createChooser(intent, "Choose an Email Service"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }