2016-03-10 130 views
0

我想在我的android應用程序中將數據庫文件上傳到Google驅動器。使用驅動器api將sql數據庫上傳到谷歌驅動器

我可以備份數據庫文件罰款保存到/ SD卡/ schoolbinder /備份/注意到

我用這個網站作爲開始:https://github.com/googledrive/android-quickstart/blob/master/app/src/main/java/com/google/android/gms/drive/sample/quickstart/MainActivity.java

這種工作方式是我的應用程序,但它圖片不是文件。關於如何上傳文件的任何想法

+0

看看[api]可能很好(https://developers.google.com/drive/v2/web/about-sdk#create_and_open_files_directly_from_the_drive_ui) –

+0

我做了上傳部分對我沒有意義在我的問題上面的這段代碼在我的應用程序中工作,它只是做圖片,而不是文件,如果有人知道如何將它製作成文件生病所有設置 – Jordan

回答

0

您引用的演示已經有選擇文件的示例。

/** 
* Create a new file and save it to Drive. 
*/ 
private void saveFileToDrive() { 
    // Start by creating a new contents, and setting a callback. 
    Log.i(TAG, "Creating new contents."); 
    final Bitmap image = mBitmapToSave; 
    Drive.DriveApi.newDriveContents(mGoogleApiClient) 
      .setResultCallback(new ResultCallback<DriveContentsResult>() { 

     @Override 
     public void onResult(DriveContentsResult result) { 
      // If the operation was not successful, we cannot do anything 
      // and must 
      // fail. 
      if (!result.getStatus().isSuccess()) { 
       Log.i(TAG, "Failed to create new contents."); 
       return; 
      } 
      // Otherwise, we can write our data to the new contents. 
      Log.i(TAG, "New contents created."); 
      // Get an output stream for the contents. 
      OutputStream outputStream = result.getDriveContents().getOutputStream(); 
      // Write the bitmap data from it. 
      ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream(); 
      image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream); 
      try { 
       outputStream.write(bitmapStream.toByteArray()); 
      } catch (IOException e1) { 
       Log.i(TAG, "Unable to write file contents."); 
      } 
      // 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("image/jpeg").setTitle("Android Photo.png").build(); 
      // Create an intent for the file chooser, and start it. 
      IntentSender intentSender = Drive.DriveApi 
        .newCreateFileActivityBuilder() 
        .setInitialMetadata(metadataChangeSet) 
        .setInitialDriveContents(result.getDriveContents()) 
        .build(mGoogleApiClient); 
      try { 
       startIntentSenderForResult(
         intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); 
      } catch (SendIntentException e) { 
       Log.i(TAG, "Failed to launch file chooser."); 
      } 
     } 
    }); 
} 

它只是一個叫它的問題。看來演示正在調用相機應用程序並返回Image。嘗試刪除onConnected方法中的if條件,應該調用saveFileToDrive並顯示文件選取器。

+0

它仍然在您上面發佈的代碼中尋找圖像 – Jordan

+0

是我的。我需要將其轉換爲文件 – Jordan

+0

您可以設置mimeType您要選擇的文件,MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() .setMimeType(「image/jpeg」)。setTitle(「Android Photo.png」) 。建立(); MimeType可以參考https://www.sitepoint.com/web-foundations/mime-types-complete-list/ – shailesh