我正在使用GitHub上提供的google drive api内容来选择文件夹。使用下面的代码,我可以获取文件夹ID来传输数据,但我无法获取文件夹名称或文件夹路径。有人可以帮忙吗?我尝试使用asDriveFolder但没有得到必要的信息。
以下是获取所选文件夹的驱动器ID的代码。
public class GoogleFolderSelector extends GoogleDriveBaseActivity {
String TAG = "Google Folder Picker";
private static final int REQUEST_CODE_OPENER = 1;
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
IntentSender intentSender = Drive.DriveApi
.newOpenFileActivityBuilder()
.setMimeType(new String[]{DriveFolder.MIME_TYPE})
.build(getGoogleApiClient());
try {
startIntentSenderForResult(
intentSender, REQUEST_CODE_OPENER, null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.w(TAG, "Unable to send intent", e);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
switch (requestCode) {
case REQUEST_CODE_OPENER:
if (resultCode == RESULT_OK) {
DriveId driveId = (DriveId) data.getParcelableExtra(
OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
DriveFolder driveFolder = driveId.asDriveFolder();
showMessage("Folder Path"+ driveFolder);
showMessage("Selected folder's ID: " + driveId);
}
finish();
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
} catch (Exception e) {
e.printStackTrace();
Popup.longpopup("Connection Established, Click to select Folder", this);
}
}
}
答案 0 :(得分:0)
为了获取文件夹名称,请使用以下元数据
Task<Metadata> getMetadataTask = getDriveResourceClient().getMetadata(file);
getMetadataTask
.addOnSuccessListener(this,
metadata -> {
showMessage(getString(
R.string.metadata_retrieved, metadata.getTitle()));
finish();
})
.addOnFailureListener(this, e -> {
Log.e(TAG, "Unable to retrieve metadata", e);
showMessage(getString(R.string.read_failed));
finish();
});
对于文件夹路径,我曾经尝试进行检索,但是似乎没有必要,因为您可以使用driveId进行操作,例如将文件添加到该driveId文件夹中或在该driveId文件夹中创建另一个文件夹。探索google sample app将带给您更好的视野。