简体   繁体   English

将文件上传到现有的Google云端硬盘文件夹-Android

[英]Upload file to existing Google Drive Folder - Android

I want to upload a file to an existing Google Drive folder. 我想将文件上传到现有的Google云端硬盘文件夹。

I am using this how to upload an image from my android app to a specific folder on google drive to get the folder name but not sure how to implement it (smokybob's answer) 我正在使用此方法如何将图像从我的android应用程序上传到Google驱动器上的特定文件夹以获取文件夹名称,但不确定如何实现(smokybob的答案)

 //Search by name and type folder
 String qStr = "mimeType = 'application/vnd.google-apps.folder' and title = 'myFolder'";

 //Get the list of Folders
 FileList fList=service.files().list().setQ(qStr).execute();

 //Check that the result is one folder
 File folder;

 if (fList.getItems().lenght==0){
 folder=fList.getItems()[0]; 
 }

 //Create the insert request is as in the sample

 File file = service.files().insert(body, mediaContent);

 //set the parent

 file.setParents(Arrays.asList(newParentReference().setId(folder.getFolderId())));

 //execute the request 

 file.execute();

I am getting cannot resolve symbol errors for FileList, body, mediacontent. 我无法解决FileList,body,mediacontent的符号错误。 I am getting cannot resolve method for .files, getitems(), setparents, newparentsreference, and execute. 我越来越无法解析.files,getitems(),setparents,newparentsreference和execute的方法。

Class:GetFile.java https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFileActivity.java 类别:GetFile.java https://github.com/googledrive/android-demos/blob/master/src/com/google/android/gms/drive/sample/demo/CreateFileActivity.java

     public class GetFile extends UploadDrive {

private static final String TAG = "CreateFileActivity";

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    // create new contents resource
    Drive.DriveApi.newDriveContents(getGoogleApiClient())
            .setResultCallback(driveContentsCallback);
}

final private ResultCallback<DriveContentsResult> driveContentsCallback = new
        ResultCallback<DriveContentsResult>() {
            @Override
            public void onResult(DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    showMessage("Error while trying to create new file contents");
                    return;
                }
                final DriveContents driveContents = result.getDriveContents();

                // Perform I/O off the UI thread.
                new Thread() {
                    @Override
                    public void run() {
                        // write content to DriveContents
                        OutputStream outputStream = driveContents.getOutputStream();
                        Writer writer = new OutputStreamWriter(outputStream);
                        try {
                            writer.write(MainActivity.driveText); //what is the problem?
                            writer.close();
                        } catch (IOException e) {
                            Log.e(TAG, e.getMessage());
                        }

                        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                .setTitle("New file")
                                .setMimeType("text/plain")
                                .setStarred(true).build();

                        // create a file on root folder
                        Drive.DriveApi.getRootFolder(getGoogleApiClient())
                                .createFile(getGoogleApiClient(), changeSet, driveContents)
                                .setResultCallback(fileCallback);
                    }
                }.start();
            }
        };

final private ResultCallback<DriveFileResult> fileCallback = new
        ResultCallback<DriveFileResult>() {
            @Override
            public void onResult(DriveFileResult result) {
                if (!result.getStatus().isSuccess()) {
                    showMessage("Error while trying to create the file");
                    return;
                }
                showMessage("Created a file with content: " + result.getDriveFile().getDriveId());
            }
        };
}

When a folder is created under GDAA, it produces a DriveId. 在GDAA下创建文件夹后,它将生成一个DriveId。

/**************************************************************************
   * create file/folder in GOODrive
   * @param prnId  parent's ID, (null or "root") for root
   * @param titl  file name
   * @param mime  file mime type
   * @param file  file (with content) to create (optional, if null, create folder)
   * @return      file id  / null on fail
   */
  static String create(String prnId, String titl, String mime, java.io.File file) {
    DriveId dId = null;
    if (mGAC != null && mGAC.isConnected() && titl != null) try {
      DriveFolder pFldr = (prnId == null || prnId.equalsIgnoreCase("root")) ?
      Drive.DriveApi.getRootFolder(mGAC):
      Drive.DriveApi.getFolder(mGAC, DriveId.decodeFromString(prnId));
      if (pFldr == null) return null; //----------------->>>

      MetadataChangeSet meta;
      if (file != null) {  // create file
        if (mime != null) {   // file must have mime
          DriveContentsResult r1 = Drive.DriveApi.newDriveContents(mGAC).await();
          if (r1 == null || !r1.getStatus().isSuccess()) return null; //-------->>>

          meta = new MetadataChangeSet.Builder().setTitle(titl).setMimeType(mime).build();
          DriveFileResult r2 = pFldr.createFile(mGAC, meta, r1.getDriveContents()).await();
          DriveFile dFil = r2 != null && r2.getStatus().isSuccess() ? r2.getDriveFile() : null;
          if (dFil == null) return null; //---------->>>

          r1 = dFil.open(mGAC, DriveFile.MODE_WRITE_ONLY, null).await();
          if ((r1 != null) && (r1.getStatus().isSuccess())) try {
            Status stts = file2Cont(r1.getDriveContents(), file).commit(mGAC, meta).await();
            if ((stts != null) && stts.isSuccess()) {
              MetadataResult r3 = dFil.getMetadata(mGAC).await();
              if (r3 != null && r3.getStatus().isSuccess()) {
                dId = r3.getMetadata().getDriveId();
              }
            }
          } catch (Exception e) {
            UT.le(e);
          }
        }

      } else {
        meta = new MetadataChangeSet.Builder().setTitle(titl).setMimeType(UT.MIME_FLDR).build();
        DriveFolderResult r1 = pFldr.createFolder(mGAC, meta).await();
        DriveFolder dFld = (r1 != null) && r1.getStatus().isSuccess() ? r1.getDriveFolder() : null;
        if (dFld != null) {
          MetadataResult r2 = dFld.getMetadata(mGAC).await();
          if ((r2 != null) && r2.getStatus().isSuccess()) {
            dId = r2.getMetadata().getDriveId();
          }
        }
      }
    } catch (Exception e) { UT.le(e); }
    return dId == null ? null : dId.encodeToString();
  } 

(must be run on non-UI thread) (必须在非UI线程上运行)
This ID is used in subsequent calls as a "parent ID". 此ID在后续调用中用作“父ID”。 If you have questions about unresolved methods, please refer to this GitHub project. 如果您对未解决的方法有疑问,请参阅此GitHub项目。 BTW (as I mentioned before), it does everything you're trying to accomplish (creates folders, creates text files, reads contents back, deletes files/folders, ...) 顺便说一句(正如我之前提到的),它可以完成您要完成的所有工作(创建文件夹,创建文本文件,读回内容,删除文件/文件夹等)。

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Android将文本文件上传到Google云端硬盘 - Upload text file to Google Drive using Android Google 驱动器文件上传在 Android Webview 中不起作用 - Google drive file upload not working in Android Webview 在文件夹中的Google Drive上上传文档 - Upload document on google drive in a folder 无法使用 REST API 在谷歌驱动器的特定文件夹中上传文件 - Unable to upload file in specific folder in google drive using REST API Google Drive Android API如何将音频文件上传到我的驱动器? 如何同步驱动器文件? - Google Drive Android API how to upload a audio file to my drive ? How to sync drive files? 如何使用python代码将Android上的文件上传到Google云端硬盘 - How to upload file on android to Google Drive using python code Java Google Drive Android API,上传后获取文件ID - Java Google Drive Android API, get file ID after upload 如何使用 API 在 Google Drive 上创建文件夹和文件? 安卓 - How to create a folder and a file on Google Drive using API? Android 如何将图像从我的Android应用程序上传到Google驱动器上的特定文件夹 - how to upload an image from my android app to a specific folder on google drive 如何使用 Java Google Drive API 创建文件夹动态和上传多个文件 - How to create folder dynamic and upload multiples file using Java Google Drive API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM