在Android中使用Drive.DriveApi。导出然后从我的Android应用内导入文件到Google Drive可以在两个方向上工作。当我首先通过浏览器上传Google无法访问Google云端硬盘文件
我也可以访问该文件并将其从Web浏览器下载到本地Mac。
但是,如果我首先使用我的Mac将新文件上传到Google云端硬盘,然后尝试从我的Android应用访问此文件并导入它,我无法访问该文件。我可以看到该文件,但它变灰了。
Im在Android应用上使用同一用户,在我的Mac上通过Google Drive联机。
有人吗?
我使用的代码。 导入:
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谷歌浏览器的驱动器:
什么范围(S)你在两个应用程序身份验证请求时使用? – muratgu
好点,试图找出我定义的范围。任何想法?在誓言握手时 – powder366
。 – muratgu