Android:如何在Google云端硬盘中获取上传的文件网址

时间:2015-03-21 13:01:56

标签: java android file-upload google-drive-api

我可以将文件上传到我的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();
}

1 个答案:

答案 0 :(得分:0)

我知道了,我们只需要获取文件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();
}