Google Drive File Creation Issue
I’m working on an Android app that generates CSV reports. The app uses ACTION_CREATE_DOCUMENT intent to let users choose where to save files. Everything worked perfectly until recently when I discovered a weird problem.
When users save files to their device’s Downloads folder, the CSV file gets created properly with all the data. But when they pick Google Drive as the destination, the file shows up as completely empty (0 bytes) even though my code runs without any errors.
I’ve tested this on multiple devices running Android 7 and 8, and they all have the same issue. Strangely, an older Android 5 device works fine with Google Drive. The URI I receive looks correct (content://com.google.android.apps.docs.storage/document/acc%3...).
Has anyone encountered this before? What could be causing Google Drive files to stay empty while local storage works fine?
Here’s my simplified test code:
public class TestActivity extends AppCompatActivity {
private static final int SAVE_FILE_REQUEST = 42;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
if (requestCode == SAVE_FILE_REQUEST && data != null) {
Uri fileUri = data.getData();
if (fileUri != null) {
new WriteFileTask(this).execute(fileUri);
}
}
}
public void exportData(View view) {
Intent createIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
createIntent.addCategory(Intent.CATEGORY_OPENABLE);
createIntent.setType("text/csv");
createIntent.putExtra(Intent.EXTRA_TITLE, "report.csv");
startActivityForResult(createIntent, SAVE_FILE_REQUEST);
}
private static class WriteFileTask extends AsyncTask<Uri, Void, Boolean> {
private final WeakReference<Context> contextRef;
WriteFileTask(Context context) {
contextRef = new WeakReference<>(context);
}
@Override
protected Boolean doInBackground(Uri... params) {
Uri targetUri = params[0];
Context ctx = contextRef.get();
if (ctx == null) return false;
String csvContent = "name,value\ntest,123\nsample,456\n";
boolean writeSuccess = false;
try {
ParcelFileDescriptor descriptor = ctx.getContentResolver().openFileDescriptor(targetUri, "w");
if (descriptor != null) {
FileOutputStream outputStream = new FileOutputStream(descriptor.getFileDescriptor());
outputStream.write(csvContent.getBytes());
outputStream.close();
descriptor.close();
writeSuccess = true;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return writeSuccess;
}
}
}