在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