gpt4 book ai didi

java - Android:如何获取 Google Drive 中上传的文件 URL

转载 作者:行者123 更新时间:2023-12-01 11:45:23 25 4
gpt4 key购买 nike

我可以将文件上传到我的 Google 云端硬盘帐户,但我需要获取文件的 URL(链接)以便稍后使用?示例:https://docs.google.com/file/d/oB-xxxxxxxxxxx/edit

这是我将文件上传到 Google 云端硬盘的方法:

private void saveFileToDrive()
{
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// Create URI from real path
String path;
path = "/sdcard/DCIM/Camera/a.png";
mFileUri = Uri.fromFile(new java.io.File(path));

ContentResolver cR = UploadActivity.this.getContentResolver();

// File's binary content
java.io.File fileContent = new java.io.File(mFileUri.getPath());
FileContent mediaContent = new FileContent(cR.getType(mFileUri), fileContent);

showToast("Selected " + mFileUri.getPath() + "to upload");

// File's meta data.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType(cR.getType(mFileUri));
com.google.api.services.drive.Drive.Files f1 = mService.files();
com.google.api.services.drive.Drive.Files.Insert i1 = f1.insert(body, mediaContent);
File file = i1.execute();

if (file != null)
{
showToast("Uploaded: " + file.getTitle());
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
showToast("Transfer ERROR: " + e.toString());
}
}
});
t.start();
}

最佳答案

我明白了,我们只需要获取文件 ID 并添加一些字符串,如下所示:

private void saveFileToDrive()
{
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
String ImageLink = null;

// Create URI from real path
String path;
path = "/sdcard/DCIM/Camera/b.mov";
mFileUri = Uri.fromFile(new java.io.File(path));

ContentResolver cR = UploadActivity.this.getContentResolver();

// File's binary content
java.io.File fileContent = new java.io.File(mFileUri.getPath());
FileContent mediaContent = new FileContent(cR.getType(mFileUri), fileContent);

showToast("Selected " + mFileUri.getPath() + " to upload");

// File's meta data.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType(cR.getType(mFileUri));
com.google.api.services.drive.Drive.Files f1 = mService.files();
com.google.api.services.drive.Drive.Files.Insert i1 = f1.insert(body, mediaContent);
File file = i1.execute();

if (file != null)
{
showToast("Uploaded: " + file.getTitle());
}

if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0)
{
ImageLink = "https://drive.google.com/open?id=" + file.getId() +"&authuser=0";
System.out.println("ImageLink: " + ImageLink);

}



} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
showToast("Transfer ERROR: " + e.toString());
}
}
});
t.start();
}

关于java - Android:如何获取 Google Drive 中上传的文件 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com