我在Android中使用Drive.DriveApi。导出然后从我的Android应用程序中导入文件到Google云端硬盘可以双向工作。
我也可以访问该文件并将其从Web浏览器下载到我的本地Mac。
但是,如果我首先使用我的Mac将新文件上传到Google云端硬盘,然后尝试从我的Android应用中访问此文件并导入它,则无法访问该文件。我可以看到该文件,但它是灰色的。
我在Android应用中使用相同的用户,在我的Mac上使用Google云端硬盘在线。
任何?
我使用的代码。 输入:
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);
}
导出:
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
}
无法选择文件vault.csv
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:
但是,如果我首先从我的Android应用中导出文件。然后从我的Mac浏览器下载文件,添加一些数据。从我的Mac上传它。 现在从我的Android应用程序导入此文件。
有没有办法通过WEB Google Drive界面更改新上传文件的权限?其他评论?
Update3我的Mac Google云端硬盘浏览器:
答案 0 :(得分:0)
将MIME类型更改为
.setMimeType(new String[]{"text/csv"})