android - 错误 : Cannot attach empty file in GMAIL app using File provider

标签 android gmail

我正在尝试附加 pdf gmail 应用程序中的文件。我读过thisthis (应用解决方案)我正在尝试;

public static void attachFile(Context ctx) {
    String TAG = "Attach";
    File documentsPath = new File(ctx.getFilesDir(), "documents");
    Log.i(TAG,"documentsAbsolutePath Output");
    Log.i(TAG, documentsPath.getAbsolutePath().toString());
    File file = new File(documentsPath, "sample.pdf");
    if ( file.exists() ) {
        Toast.makeText(ctx, "Exits", Toast.LENGTH_LONG).show();
    }else{
        Toast.makeText(ctx, "Not Exist", Toast.LENGTH_LONG).show();
    }
    Log.i(TAG,"file Output");
    Log.i(TAG, file.toString());
    Log.i(TAG, String.valueOf(file.length()));
    Uri uri = FileProvider.getUriForFile(ctx, "com.example.fyp_awais.attachfiletest2.fileprovider", file);
    Log.i(TAG,"URI Output");
    Log.i(TAG,uri.toString());
    Intent intent = ShareCompat.IntentBuilder.from((Activity) ctx)
            .setType("application/pdf")
            .setStream(uri)
            .setChooserTitle("Choose bar")
            .createChooserIntent()
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    ctx.startActivity(intent);
}

输出

documentsAbsolutePath Output
/data/data/com.example.fyp_awais.attachfiletest2/files/documents
 file Output
/data/data/com.example.fyp_awais.attachfiletest2/files/documents/sample.pdf
 0
 URI Output
 content://com.example.fyp_awais.attachfiletest2.fileprovider/pdf_folder/sample.pdf

list

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

文件路径.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <files-path name="pdf_folder" path="documents/"/>
    </paths>
</PreferenceScreen

pdf文件保存在Galaxy Core Prime\Phone\documents中. (文件大小:53.7KB)

但是它给出了

Cannot attach empty file.

enter image description here

我对这行 <files-path name="pdf_folder" path="documents/"/> 中的文件夹名称感到困惑.该文件位于 \Phone\documents .那么为什么文件夹名称?

编辑 1

试图替换 setType(application/pdf)setType("message/rfc822")但是没有用。有帮助吗?

最佳答案

像这样以 URI 格式发送文件:

Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setData(Uri.parse("mailto:"));
        emailIntent.setType("application/image");
        emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
        emailIntent.putExtra(Intent.EXTRA_CC, CC);
ArrayList<Uri> uris = new ArrayList<>();
        //convert from paths to Android friendly Parcelable Uri's
        uris.add(frontImageUri);
        uris.add(backImageUri);
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));

关于android - 错误 : Cannot attach empty file in GMAIL app using File provider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43316201/

相关文章:

android - 如何解决由 gradle 插件创建的依赖冲突?

android - 如何在 android 上播放 webrtc.AudioTrack(无视频)

java - Android 从 HTML/PHP 获取字符串

php - 从 PHP 页面使用 GMail SMTP 服务器发送电子邮件

ruby-on-rails - 如何通过 GMAIL API 发送带有 BCC header 的电子邮件?

css - 防止 gmail 应用程序 android 呈现移动 HTML 电子邮件

zend-framework - 通过 Gmail smtp 服务器使用 Zend_Mail 发送邮件

java - 使用 mapquest 在 Android 上获取路线点

email - google、yahoo、hotmail 中的虚假邮件检测失败

android - 使用 Flutter 接受 Android 许可证,无需手动确认