Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot attach empty file in GMAIL app using File provider

Tags:

android

gmail

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 Prime\Phone\documents. (file size: 53.7KB)

But it gives

Cannot attach empty file.

enter image description here

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

Edit 1

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

like image 578
Humty Avatar asked Apr 10 '17 06:04

Humty


2 Answers

Send the file in URI format like this:

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..."));
like image 163
Simranjeet Singh Avatar answered Sep 29 '22 17:09

Simranjeet Singh


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);
like image 27
Drim Avatar answered Sep 29 '22 17:09

Drim