android - 将文件上传到谷歌驱动器后如何获取谷歌驱动器文件ID?
问题描述
我正在使用 Android API 将文件上传到谷歌驱动器,但无法获取准确或有效的驱动器 ID。我得到的驱动器 ID 无法打开。
private Task<Void> createFileIntentSender(DriveContents driveContents, File file, String mimetype) {
//Log.i(TAG, "New contents created.");
// Get an output stream for the contents.
try {
ParcelFileDescriptor pfd = driveContents.getParcelFileDescriptor();
long bytesToSkip = pfd.getStatSize();
InputStream in = new FileInputStream(pfd.getFileDescriptor());
while (bytesToSkip > 0) {
long skipped = in.skip(bytesToSkip);
bytesToSkip -= skipped;
}
OutputStream oos = new FileOutputStream(pfd.getFileDescriptor());
if (oos != null)
try
{
Log.d("Testing", "input stream::: "+(inputStream==null));
byte[] buf = new byte[8192];
int c;
while ((c = inputStream.read(buf, 0, buf.length)) > 0) {
oos.write(buf, 0, c);
oos.flush();
}
}
finally
{
oos.close();
}
}
catch (Exception ignore) {
Log.d("Testing", "exception:: "+ignore.getMessage());
}
// Create the initial metadata - MIME type and title.
// Note that the user will be able to change the title later.
MetadataChangeSet metadataChangeSet =
new MetadataChangeSet.Builder()
.setMimeType(mimetype)
.setTitle(filename)
.build();
//filename = "sample"+mimetype;
// Set up options to configure and display the create file activity.
CreateFileActivityOptions createFileActivityOptions =
new CreateFileActivityOptions.Builder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(driveContents)
.build();
return mDriveClient
.newCreateFileActivityIntentSender(createFileActivityOptions)
.continueWith(
new Continuation<IntentSender, Void>() {
@Override
public Void then(@NonNull Task<IntentSender> task) throws Exception {
Log.d("Testing", "inside then::: ");
startIntentSenderForResult(task.getResult(), REQUEST_CODE_CREATOR, null, 0, 0, 0, Bundle.EMPTY);
return null;
}
});
}
现在,上传成功后,上传到驱动器,指针指向 OnActivityResult()。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CREATOR && resultCode == RESULT_OK ) {
driveId =
data.getParcelableExtra(OpenFileActivityOptions.EXTRA_RESPONSE_DRIVE_ID);
DriveFile file = driveId.asDriveFile();
Log.d("Testing", "Original driveid: " + file.getDriveId());
Log.d("Testing", "encoded driveid: " + driveId.encodeToString());
Log.d("Testing", "decoded driveid: " + decodeFromString(driveId.encodeToString()));
String s = driveId.encodeToString();
String s1 = s.substring(s.indexOf(":")+1);
s1.trim();
mStrDeviceID.add(s1);
mTvAttchment.setVisibility(View.VISIBLE);
mTvAttchment.setText(mTvAttchment.getText()+"\n"+filename);
Log.d("Testing", "File created with ID: " + driveId.encodeToString()+" "+filename);
//showMessage(getString(R.string.file_created, "File created with ID: " + driveId));
}
}
这里我得到的驱动器 ID 是这样的:DriveId:CAESABieXXD2t5T8jFooAA==。使用此驱动器 ID,我无法打开该 URL。
通常,如果我们使用此链接附加驱动器文件 ID:https ://drive.google.com/uc?id= 它会起作用。
但它不起作用。
有人可以帮忙吗。
谢谢,阿林丹。
解决方案
推荐阅读
- ruby-on-rails - 代理上的 Rails HTTP 请求中继器并返回响应
- jenkins - 如何在声明性管道中使用矩阵部分
- python - 尝试使用 Pandas Lambda 更改行值时出现语法错误
- excel - 使用excel VBA从字符串数组中删除重复项
- vue.js - 注销未从本地存储或状态清除令牌
- git - 如何禁用git删除分支的能力?
- django - 基于 CharField 用 ForeignKey 替换 CharField
- java - 如何更改 barChart ApachePoi 中文本的颜色
- mysql - 确定是否在一个线程 MySQL 中读取所有消息
- qt - 如何替换或重新定义父类的内联组件