I have an app that exports a CSV file using the `ACTION_CREATE_DOCUMENT` intent to get a Uri to write to. This was working fine for some time, but recently when I went to save a file and selected Google Drive I noticed that the resulting file was empty (0 bytes). However, if I select the Downloads folder as the destination (local storage) the file is created and written to correctly.

[![Result of saving to Google Drive (correctly written file is 14 bytes)](https://i.stack.imgur.com/PNUHB.png)](https://i.stack.imgur.com/PNUHB.png)

I condensed the problem down to a simple test Activity (below) and verified that all parts of the code are in fact being executed (no caught exceptions, `ParcelFileDescriptor` is not null, etc...). 

I tested this on a Android 7 and 8 emulators and an Android 8 physical device and all showed the same behavior, but an old Android 5 phone worked correctly (??).

The full version of the app has, and correctly requests, read and write external storage permissions to handle some [third party file managers](https://stackoverflow.com/questions/50826019/is-read-external-storage-permission-ever-required-to-read-uri-from-intent-action/50826220#50826220), but that isn't needed for Google Drive so I've omitted it from this example (behavior is the same with the permissions granted).

The uri I get looks like a valid content Uri (`content://com.google.android.apps.docs.storage/document/acc%3...`)

**Has anyone else seen this issue? Am I doing something wrong here? How can I make this work with Google Drive again?**

```
public class MainActivity extends AppCompatActivity {

    private static final int FILE_EXPORT_REQUEST_CODE = 12;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode != RESULT_OK)
            return;

        switch(requestCode) {
            case FILE_EXPORT_REQUEST_CODE:
                if( data != null ) {
                    Uri uri = data.getData();
                    if( uri != null ) {
                        new ExportCSV(this).execute(uri);
                    }
                }
                break;
        }
    }

    // test Activity has a single button that calls this
    public void saveFile(View v) {
        Intent exportIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        exportIntent.addCategory(Intent.CATEGORY_OPENABLE);
        exportIntent.setType("text/csv");
        String filename = "test.csv";
        exportIntent.putExtra(Intent.EXTRA_TITLE, filename);
        startActivityForResult(exportIntent, FILE_EXPORT_REQUEST_CODE);
    }

    private static class ExportCSV extends AsyncTask<Uri, Void, Boolean> {
        private final WeakReference<Context> context;

        ExportCSV(Context c) {
            context = new WeakReference<>(c);
        }

        @Override
        protected Boolean doInBackground(Uri... uris) {
            Uri uri = uris[0];
            Context c = context.get();

            if( c == null ) {
                return false;
            }

            String data = "colA,colB\n1,2\n";
            boolean success = false;

            try {
                ParcelFileDescriptor pfd = c.getContentResolver().openFileDescriptor(uri, "w");
                if( pfd != null ) {
                    FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
                    fileOutputStream.write(data.getBytes());
                    fileOutputStream.close();
                    success = true;
                }
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            return success;
        }
    }
}

```