我正在使用Android API将文件上传到Google驱动器,但无法获取确切或有效的驱动器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= 它会起作用。
但是它不起作用。
有人可以帮忙吗?
谢谢, 阿林丹。