Search code examples
androidgoogle-drive-apigoogle-drive-android-api

Can't access a Google Drive file from within Android when I uploaded it first via browser


Im using the Drive.DriveApi in Android. Exporting and then Importing a file from within my Android App to Google Drive works in both directions.

I can also access the file and download it from a Web browser to my local Mac.

However if I first upload a new file to Google Drive with my Mac and then later try to access this file from my Android App and Import it, I can't access the file. I can see the file, but it is greyed out.

Im using the same user both on the Android App as online on my Mac with Google Drive.

Anyone?

My code I use. Import:

        IntentSender intentSender = Drive.DriveApi
                .newOpenFileActivityBuilder()
                .setMimeType(new String[]{"application/csv"})
                .build(activity.getGoogleClient());
        try {
            activity.startIntentSenderForResult(intentSender, DRIVE_REQUEST_CODE_OPENER_IMPORT, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            Log.w(TAG, "Unable to send intent: ", e);
        }

Export:

        Drive.DriveApi.newDriveContents(activity.getGoogleClient()).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
                Uri csvUri = Uri.parse("file://" + getCsvFile(context));
                File csvFile = new File(csvUri.getPath());
                if(csvFile.length() <= 0) {
                    Log.e(TAG, "File empty: " + getCsvFile(context));
                }
                FileInputStream csvInputStream = null;
                try {
                    csvInputStream  = new FileInputStream(csvFile);
                    OutputStream outputStream = driveContentsResult.getDriveContents().getOutputStream();
                    byte[] dataArray = IOUtils.toByteArray(csvInputStream);
                    outputStream.write(dataArray);
                } catch (FileNotFoundException fnfe) {
                    Log.e(TAG, "File not found: ", fnfe);
                } catch (IOException ie) {
                    Log.e(TAG, "Error uploading file: ", ie);
                }
                MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder().setMimeType("application/csv").setTitle(getCsvFilename()).build();
                IntentSender intentSender = Drive.DriveApi
                        .newCreateFileActivityBuilder()
                        .setInitialMetadata(metadataChangeSet)
                        .setInitialDriveContents(driveContentsResult.getDriveContents())
                        .build(activity.getGoogleClient());
                try {
                    activity.startIntentSenderForResult(intentSender, DRIVE_REQUEST_CODE_OPENER_EXPORT, null, 0, 0, 0);
                } catch (IntentSender.SendIntentException e) {
                    Log.w(TAG, "Unable to send intent: ", e);
                }
            }
        });

onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    final Uri csvUri = Uri.parse("file://" + getCsvFile(context));
    final File csvFile = new File(csvUri.getPath());
    if(requestCode == DRIVE_REQUEST_CODE_OPENER_IMPORT && resultCode == activity.RESULT_OK && data != null) {
        DriveId driveId = (DriveId) data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
        Log.i(TAG, "Selected file's ID: " + driveId);
        driveId.asDriveFile().open(activity.getGoogleClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
                try {
                    InputStream input = driveContentsResult.getDriveContents().getInputStream();
                    byte[] dataArray = IOUtils.toByteArray(input);
                    FileUtils.writeByteArrayToFile(csvFile, dataArray);
                    finish.onImportFinished();
                } catch (IOException ie) {
                    Log.e(TAG, "Error downloading file: ", ie);
                }
            }
        });
    } else if(requestCode == DRIVE_REQUEST_CODE_OPENER_EXPORT && resultCode != activity.RESULT_CANCELED) {
        finish.onExportFinished();
    }
    csvFile.delete();   // Always delete to avoid readable file on disk
}

Can't select file vault.csv

enter image description here

Update1:

public void initGoogleClient() {
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    mGoogleApiClient.connect();
}

Update2:

If I however first export a file from within my Android app. Then download the file from my browser on my Mac, add some data. Upload it from my Mac. Now importing this file from my Android app works.

Is there a way to change the privileges on a newly uploaded file via the WEB Google Drive interface? Other comments?

Update3 my Mac Google Drive browser:

enter image description here


Solution

  • Changed MIME type to

      .setMimeType(new String[]{"text/csv"})