GDAA(Google Drive Android API): 使用 fileId 从 AppDataFolder 下载文件
GDAA(Google Drive Android API): download file from AppDataFolder by using fileId
我在我的应用程序中使用 google 驱动器。我能够在 Google drive App folder
中上传数据并获取文件 ID。我的问题是,如何使用 FileId
从 appDataFolder
下载它
这就是我获取文件 ID 的方式
private List<String> getDataFromApi() throws IOException {
List<String> fileInfo = new ArrayList<String>();
FileList lst = mService.files().list()
.setSpaces("appDataFolder")
.execute();
FileList result = mService.files().list()
.setSpaces("appDataFolder")
.setFields("nextPageToken, files(id, name, mimeType)")
.execute();
List<File> files = result.getFiles();
if (files != null) {
for (File file : files) {
fileInfo.add(String.format("%s (%s) %s\n",
file.getName(), file.getId(), file.getMimeType()));
}
}
return fileInfo;
}
我正在使用 v3 的 google 驱动服务。
compile('com.google.apis:google-api-services-drive:v3-rev102-1.23.0') {
exclude group: 'org.apache.httpcomponents'
}
提前致谢
我终于得到了答案。希望这对其他人也能从 appDataFolder
下载有所帮助。我在这里下载文件并将其保存到 SD card
文件夹名称 Spectors
中。并传递文件类型 com.google.api.services.api.drive.model.File
的值。
private void downloadFile(boolean useDirectDownload, File uploadedFile)
throws IOException {
java.io.File direct = new java.io.File(Environment.getExternalStorageDirectory()
+ "/Spectors");
if (!direct.exists()) {
direct.mkdirs();
}
java.io.File parentDir = new java.io.File(direct.getAbsolutePath());
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getName()+".zip"));
com.google.api.services.drive.Drive.Files.Get files = mService.files().get(uploadedFile.getId());
files.executeMediaAndDownloadTo(out);
Log.e(TAG, "downloadFile:2 " + files.toString() + "\n"+uploadedFile.getFileExtension() );
}
您也可以使用以下方法从应用文件夹下载内容
通过以下代码从应用程序文件夹中获取文件
private void listFiles() {
Query query =
new Query.Builder()
.addFilter(Filters.or(Filters.eq(SearchableField.MIME_TYPE, "text/html"),
Filters.eq(SearchableField.MIME_TYPE, "text/plain")))
.build();
getDriveResourceClient()
.query(query)
.addOnSuccessListener(this,
new OnSuccessListener<MetadataBuffer>() {
@Override
public void onSuccess(MetadataBuffer metadataBuffer) {
//mResultsAdapter.append(metadataBuffer);
for (int i = 0; i <metadataBuffer.getCount() ; i++) {
retrieveContents(metadataBuffer.get(i).getDriveId().asDriveFile());
}
}
}
)
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Error retrieving files", e);
MainActivity.this.finish();
}
});
}
通过以下代码下载内容
private void retrieveContents(DriveFile file) {
// [START drive_android_open_file]
Task<DriveContents> openFileTask =
getDriveResourceClient().openFile(file, DriveFile.MODE_READ_ONLY);
// [END drive_android_open_file]
// [START drive_android_read_contents]
openFileTask
.continueWithTask(task -> {
DriveContents contents = task.getResult();
// Process contents...
// [START_EXCLUDE]
// [START drive_android_read_as_string]
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(contents.getInputStream()))) {
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
showMessage(getString(R.string.content_loaded));
mFileContents.setText(builder.toString());
}
// [END drive_android_read_as_string]
// [END_EXCLUDE]
// [START drive_android_discard_contents]
Task<Void> discardTask = getDriveResourceClient().discardContents(contents);
// [END drive_android_discard_contents]
return discardTask;
})
.addOnFailureListener(e -> {
// Handle failure
// [START_EXCLUDE]
Log.e(TAG, "Unable to read contents", e);
showMessage(getString(R.string.read_failed));
finish();
// [END_EXCLUDE]
});
// [END drive_android_read_contents]
}
我在我的应用程序中使用 google 驱动器。我能够在 Google drive App folder
中上传数据并获取文件 ID。我的问题是,如何使用 FileId
从 appDataFolder
下载它
这就是我获取文件 ID 的方式
private List<String> getDataFromApi() throws IOException {
List<String> fileInfo = new ArrayList<String>();
FileList lst = mService.files().list()
.setSpaces("appDataFolder")
.execute();
FileList result = mService.files().list()
.setSpaces("appDataFolder")
.setFields("nextPageToken, files(id, name, mimeType)")
.execute();
List<File> files = result.getFiles();
if (files != null) {
for (File file : files) {
fileInfo.add(String.format("%s (%s) %s\n",
file.getName(), file.getId(), file.getMimeType()));
}
}
return fileInfo;
}
我正在使用 v3 的 google 驱动服务。
compile('com.google.apis:google-api-services-drive:v3-rev102-1.23.0') {
exclude group: 'org.apache.httpcomponents'
}
提前致谢
我终于得到了答案。希望这对其他人也能从 appDataFolder
下载有所帮助。我在这里下载文件并将其保存到 SD card
文件夹名称 Spectors
中。并传递文件类型 com.google.api.services.api.drive.model.File
的值。
private void downloadFile(boolean useDirectDownload, File uploadedFile)
throws IOException {
java.io.File direct = new java.io.File(Environment.getExternalStorageDirectory()
+ "/Spectors");
if (!direct.exists()) {
direct.mkdirs();
}
java.io.File parentDir = new java.io.File(direct.getAbsolutePath());
if (!parentDir.exists() && !parentDir.mkdirs()) {
throw new IOException("Unable to create parent directory");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, uploadedFile.getName()+".zip"));
com.google.api.services.drive.Drive.Files.Get files = mService.files().get(uploadedFile.getId());
files.executeMediaAndDownloadTo(out);
Log.e(TAG, "downloadFile:2 " + files.toString() + "\n"+uploadedFile.getFileExtension() );
}
您也可以使用以下方法从应用文件夹下载内容
通过以下代码从应用程序文件夹中获取文件
private void listFiles() {
Query query =
new Query.Builder()
.addFilter(Filters.or(Filters.eq(SearchableField.MIME_TYPE, "text/html"),
Filters.eq(SearchableField.MIME_TYPE, "text/plain")))
.build();
getDriveResourceClient()
.query(query)
.addOnSuccessListener(this,
new OnSuccessListener<MetadataBuffer>() {
@Override
public void onSuccess(MetadataBuffer metadataBuffer) {
//mResultsAdapter.append(metadataBuffer);
for (int i = 0; i <metadataBuffer.getCount() ; i++) {
retrieveContents(metadataBuffer.get(i).getDriveId().asDriveFile());
}
}
}
)
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Error retrieving files", e);
MainActivity.this.finish();
}
});
}
通过以下代码下载内容
private void retrieveContents(DriveFile file) {
// [START drive_android_open_file]
Task<DriveContents> openFileTask =
getDriveResourceClient().openFile(file, DriveFile.MODE_READ_ONLY);
// [END drive_android_open_file]
// [START drive_android_read_contents]
openFileTask
.continueWithTask(task -> {
DriveContents contents = task.getResult();
// Process contents...
// [START_EXCLUDE]
// [START drive_android_read_as_string]
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(contents.getInputStream()))) {
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
showMessage(getString(R.string.content_loaded));
mFileContents.setText(builder.toString());
}
// [END drive_android_read_as_string]
// [END_EXCLUDE]
// [START drive_android_discard_contents]
Task<Void> discardTask = getDriveResourceClient().discardContents(contents);
// [END drive_android_discard_contents]
return discardTask;
})
.addOnFailureListener(e -> {
// Handle failure
// [START_EXCLUDE]
Log.e(TAG, "Unable to read contents", e);
showMessage(getString(R.string.read_failed));
finish();
// [END_EXCLUDE]
});
// [END drive_android_read_contents]
}