ardevd
ardevd

Reputation: 3407

FileProvider: No data when sending file

I've configured a FileProvider and I'm trying to send files but the external applications (Google Drive, etc) handling the intents throw an error saying that the intent contained no data. What am I missing here?

            File backupPath = new File(getContext().getFilesDir(), "backups");
            File backupFile = new File(backupPath, clickedBackup.getFilename());
            Uri contentUri = getUriForFile(getContext(), "com.test.app.fileprovider", backupFile);

            // create new Intent
            Intent intent = new Intent(Intent.ACTION_SEND);
            // set flag to give temporary permission to external app to use your FileProvider
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(
                    contentUri,
                    getContext().getContentResolver().getType(contentUri));
            // validate that the device can open your File!
            PackageManager pm = getActivity().getPackageManager();
            if (intent.resolveActivity(pm) != null) {
                startActivity(intent);
            }

Logcat shows the following:

12-18 12:47:55.554  1698  2490 I ActivityManager: START u0 {act=android.intent.action.SEND dat=content://com.test.app.fileprovider/backups/20171918_121910.bak typ=application/octet-stream flg=0x3000001 cmp=com.google.android.apps.docs/.shareitem.UploadMenuActivity} from uid 10079

The content URI looks good to me but for some reason it's not working. Any ideas?

Here is the provider paths.

<paths>
    <files-path name="backups" path="backups/" />
</paths>

And finally, the provider declaration.

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

UPDATE: logcat also shows the following which may or not be key to the problem here

12-18 13:21:23.739  4818  6764 E DataSourceHelper: Uploading single file but neither EXTRA_STREAM nor EXTRA_TEXT is specified.
12-18 13:21:23.790  1431  1483 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496
12-18 13:21:23.796  1431  1483 I chatty  : uid=1000(system) HwBinder:1431_1 identical 1 line
12-18 13:21:23.807  1431  1483 D gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496
12-18 13:21:23.824  4818  4818 E UploadMenuActivity: No files requested to be uploaded: android.intent.action.SEND

EDIT 2: Adding this makes it work just fine:

intent.putExtra(Intent.EXTRA_STREAM, contentUri);

Upvotes: 4

Views: 979

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006554

Change:

intent.setDataAndType(contentUri, getContext().getContentResolver().getType(contentUri));

to:

intent.setType(contentUri, "application/octet-stream");
intent.putExtra(Intent.EXTRA_STREAM, contentUri);

ACTION_SEND uses EXTRA_STREAM — not the Uri facet of the Intent — for sending over the Uri. And since you know the MIME type, there is no sense in using IPC calls to derive it via ContentResolver.

Upvotes: 6

Nikhil Lotke
Nikhil Lotke

Reputation: 665

I think you have to set type which is missing. Try adding below:

intent.setType("image/*|application/pdf|audio/*");

for more details visit https://developer.android.com/training/sharing/send.html

Upvotes: -2

Related Questions