2014-10-01 89 views
1

我必須在我的Android應用程序中實現同步。所以,我需要使用用戶的谷歌驅動器來存儲和檢索信息。我創建了將* .db文件存儲到驅動器的功能。但問題是用戶每次都必須確認文件上傳操作。我想在沒有確認的情況下關閉每個應用程序上傳文件。可能嗎 ?有沒有其他的方式來同步sqlite數據庫,而沒有自己的服務器?同步SQLite數據庫到谷歌驅動器沒有確認

public static void backUpDb(final Activity context){ 
    mGoogleApiClient = new GoogleApiClient.Builder(context) 
    .addApi(Drive.API) 
    .addScope(Drive.SCOPE_FILE) 
    .addConnectionCallbacks(new ConnectionCallbacks() { 
     @Override 
     public void onConnectionSuspended(int arg0) { 
     } 

     @Override 
     public void onConnected(Bundle arg0) { 

      Log.i(TAG, "API client connected."); 
      saveFileToDrive(context); 
     } 
    }) 
    .addOnConnectionFailedListener(
      new OnConnectionFailedListener() { 

       @Override 
       public void onConnectionFailed(
         ConnectionResult result) { 
        Log.i(TAG, 
          "GoogleApiClient connection failed: " 
            + result.toString()); 
        if (!result.hasResolution()) { 
         // show the localized error 
         // dialog. 
         GooglePlayServicesUtil 
           .getErrorDialog(
             result.getErrorCode(), 
             context, 0).show(); 
         return; 
        } 

        try { 
         result.startResolutionForResult(
           context, 
           REQUEST_CODE_RESOLUTION); 
        } catch (SendIntentException e) { 
         Log.e(TAG, 
           "Exception while starting resolution activity", 
           e); 
        } 

       } 
      }).build(); 
    mGoogleApiClient.connect(); 


    //mGoogleApiClient.disconnect();   
} 



public static void saveFileToDrive(final Activity context) { 
    Drive.DriveApi.newContents(mGoogleApiClient).setResultCallback(
      new ResultCallback<ContentsResult>() { 

       @Override 
       public void onResult(ContentsResult result) { 
        if (!result.getStatus().isSuccess()) { 

         return; 
        } 
        OutputStream outputStream = result.getContents() 
          .getOutputStream(); 
        try { 
         // outputStream.write(bitmapStream.toByteArray()); 

         //ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream(); 
         File dbFile = context.getDatabasePath(databaseName); 
         //dbFile. 
         int size = (int) dbFile.length(); 
         byte[] bytes = new byte[size]; 
         BufferedInputStream buf = new BufferedInputStream(new FileInputStream(dbFile)); 
         buf.read(bytes, 0, bytes.length); 
         buf.close(); 

         byte[] b = "aa".getBytes(); 
         outputStream.write(bytes); 
        } catch (IOException e1) { 
         Log.i(TAG, "Unable to write file contents."); 
        } 

        String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db"); 
        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() 
          .setMimeType(mimeType).setTitle("aldkan.db") 
          .build(); 
        IntentSender intentSender = Drive.DriveApi 
          .newCreateFileActivityBuilder() 
          .setInitialMetadata(metadataChangeSet) 
          .setInitialContents(result.getContents()) 
          .build(mGoogleApiClient); 
        try { 
         context.startIntentSenderForResult(intentSender, 2, 
           null, 0, 0, 0); 
        } catch (SendIntentException e) { 

         e.printStackTrace(); 
        } 
       } 
      }); 
} 

回答

0

CreateFileActivity作爲構建Google Drive的文件上傳UI的便捷方式提供。但是,您可以直接使用API​​創建和修改文件,如指南中所述,以避免任何用戶交互。

相關問題