错误:无法使用文件提供程序在 GMAIL 应用程序中附加空文件

Error: Cannot attach empty file in GMAIL app using File provider(错误:无法使用文件提供程序在 GMAIL 应用程序中附加空文件)

本文介绍了错误:无法使用文件提供程序在 GMAIL 应用程序中附加空文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 gmail 应用程序中附加 pdf 文件.我已阅读

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

编辑 1

尝试将 setType(application/pdf) 替换为 setType("message/rfc822") 但没有奏效.有什么帮助吗?

解决方案

如果可以发Zip就试试这个方法

 String fileNameStor_zip = Environment.getExternalStorageDirectory() + "/";+ 文件名 + ".zip";String[] path = {你的第一个 pdf 文件路径,你的第二个 pdf 文件路径};压缩 compress = new Compress(path, fileNameStor_zip);压缩.zip();URI = Uri.parse("file://" + fileNameStor_zip);

提供您的 Gmail Intent

intent.putExtra(Intent.EXTRA_STREAM, URI);

I am trying to attach a pdf file in gmail app. I have read this and this (applied solution) I am trying as;

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);
}

Outputs

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

Menifest

<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>

FilePath.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

A pdf file is saved in Galaxy Core PrimePhonedocuments. (file size: 53.7KB)

But it gives

Cannot attach empty file.

I am confused with folder-name in this line <files-path name="pdf_folder" path="documents/"/>. The file is in the Phonedocuments. Then why folder name?

Edit 1

Tried to replace setType(application/pdf) with setType("message/rfc822") But did not work. Any help?

解决方案

if it's ok To send Zip then try this way

 String fileNameStor_zip = Environment.getExternalStorageDirectory() + "/" + fileName + ".zip";

String[] path = { your 1st pdf  File Path, your 2nd pdf  File Path};

Compress compress = new Compress(path, fileNameStor_zip);

compress.zip();


URI =  Uri.parse("file://" + fileNameStor_zip);

Provide your Gmail Intent

intent.putExtra(Intent.EXTRA_STREAM, URI);

这篇关于错误:无法使用文件提供程序在 GMAIL 应用程序中附加空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:错误:无法使用文件提供程序在 GMAIL 应用程序中附加空文件

基础教程推荐