簡體   English   中英

將文件上傳到現有的Google雲端硬盤文件夾-Android

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

我想將文件上傳到現有的Google雲端硬盤文件夾。

我正在使用此方法如何將圖像從我的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();

我無法解決FileList,body,mediacontent的符號錯誤。 我越來越無法解析.files,getitems(),setparents,newparentsreference和execute的方法。

類別: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());
            }
        };
}

在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();
  } 

(必須在非UI線程上運行)
此ID在后續調用中用作“父ID”。 如果您對未解決的方法有疑問,請參閱此GitHub項目。 順便說一句(正如我之前提到的),它可以完成您要完成的所有工作(創建文件夾,創建文本文件,讀回內容,刪除文件/文件夾等)。

祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM