【问题标题】:Download database File For Google Drive API Android为 Google Drive API Android 下载数据库文件
【发布时间】:2016-04-18 22:47:28
【问题描述】:

我正在使用驱动器 api 在谷歌驱动器上的隐藏应用程序文件夹中创建一个数据库文件。数据库文件名为 notes.db 我已经能够成功地将数据库文件上传到谷歌驱动器,但我不知道如何将其下载回用户的设备。这就是我想要做的。我的应用程序在用户设备上创建了一个名为 School Binder 的文件夹。在该文件夹中是另一个名为 Note 备份的文件夹。这是我备份数据库的地方。目录是

 Environment.getExternalStorageDirectory() + "/School Binder/Note Backups/Notes.db"

Google 云端硬盘获取此文件并将其上传到隐藏的应用文件夹。现在我想把这个 notes.db 文件存储在谷歌驱动器上的那个 app 文件夹中,然后把它下载回手机上的这个目录。

 Environment.getExternalStorageDirectory() + "/School Binder/Note Backups/Notes.db"

我该怎么做。谢谢。这是我上传数据库以正常工作的代码

        // Define And Instantiate Variable DriveContents driveContents//
        DriveContents driveContents = result.getStatus().isSuccess() ? result.getDriveContents() : null;

        // Gets The Data for The File//
        if (driveContents != null) try {

            // Define And Instantiate Variable OutputStream outputStream//
            OutputStream outputStream = driveContents.getOutputStream();

            // Start Writing Data To File//
            if (outputStream != null) try {

                // Define And Instantiate Variable InputStream inputStream//
                InputStream inputStream = new FileInputStream(dbFile);

                // Define And Instantiate Variable Byte buffer//
                byte[] buffer = new byte[5000];

                // Define Variable Int data//
                int data;

                // Run Code While data Is Bigger Then Zero//
                while ((data = inputStream.read(buffer, 0, buffer.length)) > 0) {

                    // Write To outputStream//
                    outputStream.write(buffer, 0, data);

                    // Flush outputStream//
                    outputStream.flush();
                }

            } finally {

                // Close outputStream//
                outputStream.close();
            }

        } catch (Exception e) {e.printStackTrace(); Toast.makeText(getApplicationContext(), "Failed To Upload: No Backup File Found", Toast.LENGTH_LONG).show(); return;}

如何更改它以使其能够从谷歌驱动器下载数据到文件

【问题讨论】:

标签: android mysql google-drive-android-api


【解决方案1】:

Lifecycle of a Drive file 中,即使设备处于离线状态,Drive Android API 也可以让您的应用访问文件。为了支持离线情况,API 实现了一个同步引擎,该引擎在后台运行以在网络访问可用时进行上游和下游更改并解决冲突。如果文件尚未同步到本地上下文但用户想要打开文件,则执行初始下载请求。当请求文件时,API 会自动处理。

downloading a file 中,您向文件的资源URL 发出授权的HTTP GET 请求,并包含查询参数alt=media。但请注意,下载文件要求用户至少具有读取权限。

HTTP 请求示例:

获取https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media 授权:承载 ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs

对于编码部分,这个SO post 可能也有帮助。

【讨论】:

    【解决方案2】:

    我发现这是我将数据库重新下载回手机的代码

            //<editor-fold desc="Create Drive Db File On Device">
    
            // Log That The File Was Opened//
            Log.d("TAG", "File contents opened");
    
            // Define And Instantiate Variable DriveContents driveContents//
            DriveContents driveContents = result.getStatus().isSuccess() ? result.getDriveContents() : null;
    
            // Gets The Data for The File//
            if (driveContents != null) try {
    
                // Define And Instantiate Variable OutputStream outputStream//
                OutputStream outputStream = new FileOutputStream(dbFile);
    
                // Define And Instantiate Variable InputStream inputStream//
                InputStream inputStream = driveContents.getInputStream();
    
                // Define And Instantiate Variable Byte buffer//
                byte[] buffer = new byte[5000];
    
                // Define Variable Int data//
                int data;
    
                // Run Code While data Is Bigger Then Zero//
                while ((data = inputStream.read(buffer, 0, buffer.length)) > 0) {
    
                    // Write To outputStream//
                    outputStream.write(buffer, 0, data);
    
                    // Flush outputStream//
                    outputStream.flush();
                }
    
                // Close outputStream//
                outputStream.close();
    
                // Discard Drive Contents//
                driveContents.discard(googleApiClient);
    
            } catch (Exception e) {e.printStackTrace(); Toast.makeText(getApplicationContext(), "File Failed To Download", Toast.LENGTH_LONG).show(); }
    
            //</editor-fold>
    

    【讨论】:

      【解决方案3】:

      这是一个完整的类,用于上传内部数据库、下载并从 Google Drive 中删除。

      只需要异步调用函数并向用户显示进度条。 DownloadFromGoogleDrive 函数将内部数据库文件夹中的数据库保存到名为“database2”的应用中

      希望对你有帮助。

      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      import com.google.android.gms.common.ConnectionResult;
      import com.google.android.gms.common.api.GoogleApiClient;
      import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
      import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
      import com.google.android.gms.common.api.ResultCallback;
      import com.google.android.gms.common.api.Status;
      import com.google.android.gms.drive.Drive;
      import com.google.android.gms.drive.DriveApi;
      import com.google.android.gms.drive.DriveApi.MetadataBufferResult;
      import com.google.android.gms.drive.DriveFile.DownloadProgressListener;
      import com.google.android.gms.drive.DriveId;
      import com.google.android.gms.drive.DriveResource;
      import com.google.android.gms.drive.Metadata;
      import com.google.android.gms.drive.DriveApi.DriveContentsResult;
      import com.google.android.gms.drive.DriveContents;
      import com.google.android.gms.drive.DriveFile;
      import com.google.android.gms.drive.DriveFolder.DriveFileResult;
      import com.google.android.gms.drive.MetadataChangeSet;
      import com.google.android.gms.drive.query.Filters;
      import com.google.android.gms.drive.query.Query;
      import com.google.android.gms.drive.query.SearchableField;
      
      import android.app.Activity;
      import android.content.Intent;
      import android.content.IntentSender.SendIntentException;
      import android.os.Bundle;
      import android.util.Log;
      import android.webkit.MimeTypeMap;
      import android.widget.Toast;
      
      public class BackupDatabaseActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
      
      private static final String TAG = "BackupDatabaseActivity";
      private GoogleApiClient api;
      private boolean mResolvingError = false;
      private static final int DIALOG_ERROR_CODE =100;
      private static final String DATABASE_NAME = "database";
      private static final String GOOGLE_DRIVE_FILE_NAME = "database_backup";
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          // Create the Drive API instance
          api = new GoogleApiClient.Builder(this).addApi(Drive.API).addScope(Drive.SCOPE_FILE).
                  addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
      }
      
      
      
      final private ResultCallback<DriveApi.DriveContentsResult> contentsCallback = new ResultCallback<DriveApi.DriveContentsResult>() {
      
          @Override
          public void onResult(DriveApi.DriveContentsResult result) {
              if (!result.getStatus().isSuccess()) {
                  Log.v(TAG, "Error while trying to create new file contents");
                  return;
              }
              CreateFileOnGoogleDrive(result);
              //OR DownloadFromGoogleDrive(result);
              //OR DeleteFromGoogleDrive(result);
          }
      
      };
      
      
      final private ResultCallback<DriveFileResult> fileCallback = new ResultCallback<DriveFileResult>() {
      
          @Override
          public void onResult(DriveFileResult result) {
              if (!result.getStatus().isSuccess()) {
                  Log.v(TAG, "Error while trying to create the file");
                  return;
              }
      
              Log.v(TAG, "File created: "+result.getDriveFile().getDriveId());
          }
      };
      
      /**
       * Create a file in root folder using MetadataChangeSet object.
       * @param result
       */
      public void CreateFileOnGoogleDrive(DriveContentsResult result){
          final DriveContents driveContents = result.getDriveContents();
      
          // Perform I/O off the UI thread.
          new Thread() {
              @Override
              public void run() {
                  try {
                      FileInputStream is = new FileInputStream(getDbPath());
                      BufferedInputStream in = new BufferedInputStream(is);
                      byte[] buffer = new byte[8 * 1024];
                      BufferedOutputStream out = new BufferedOutputStream(driveContents.getOutputStream());
                      int n = 0;
                      while( ( n = in.read(buffer) ) > 0 ) {
                          out.write(buffer, 0, n);
                      }
                      out.flush();
                      out.close();
                      in.close();
                  } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");
                  MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                          .setTitle(GOOGLE_DRIVE_FILE_NAME) // Google Drive File name
                          .setMimeType(mimeType)
                          .setStarred(true).build();
      
                  // create a file in root folder
                  Drive.DriveApi.getRootFolder(api)
                          .createFile(api, changeSet, driveContents)
                          .setResultCallback(fileCallback);
              }
          }.start();
      }
      
      /**
       * Download File from Google Drive
       * @param result
       */
      public void DownloadFromGoogleDrive(DriveContentsResult result){
          final DriveContents driveContents = result.getStatus().isSuccess() ? result.getDriveContents() : null;
          if(driveContents!=null){
              Query query = new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, GOOGLE_DRIVE_FILE_NAME)).build();
              Drive.DriveApi.query(api, query).setResultCallback(new ResultCallback<MetadataBufferResult>() {
      
                  @Override
                  public void onResult(MetadataBufferResult result) {
                      try{
                          DriveId driveId = result.getMetadataBuffer().get(0).getDriveId();
                          DriveFile driveFile =  driveId.asDriveFile();
                          //mProgressBar.setProgress(0);
                          DownloadProgressListener listener = new DownloadProgressListener() {
                              @Override
                              public void onProgress(long bytesDownloaded, long bytesExpected) {
                                  // Update progress dialog with the latest progress.
                                  int progress = (int)(bytesDownloaded*100/bytesExpected);
                                  Log.d(TAG, String.format("Loading progress: %d percent", progress));
                                 // mProgressBar.setProgress(progress);
                              }
                          };
      
                          driveFile.open(api, DriveFile.MODE_READ_ONLY, listener).setResultCallback(driveContentsCallback);
      
                      }catch(Exception e){
                          Toast.makeText(getApplicationContext(), "File Failed To Download", Toast.LENGTH_LONG).show(); 
                      }
                  }
              });
          }else{
              Toast.makeText(getApplicationContext(), "File Failed To Download", Toast.LENGTH_LONG).show(); 
          }
      }
      
      private ResultCallback<DriveContentsResult> driveContentsCallback =
              new ResultCallback<DriveContentsResult>() {
          @Override
          public void onResult(DriveContentsResult result) {
              if (!result.getStatus().isSuccess()) {
                  Log.d(TAG, "Error while opening the file contents");
                  return;
              }
              Log.d(TAG, "Downloaded");
              DriveContents dc = result.getDriveContents();            
              try {
                  InputStream inputStream = dc.getInputStream();
      
                  OutputStream outputStream = new FileOutputStream(getDbPath()+"2");
      
                  byte[] buffer = new byte[8 * 1024];
                  //BufferedOutputStream out = new BufferedOutputStream(dc.getOutputStream());
                  int n = 0;
                  while( ( n = inputStream.read(buffer) ) > 0 ) {
                      outputStream.write(buffer, 0, n);
                  }
                  outputStream.flush();
                  outputStream    .close();
                  //inputStream.close();
                  dc.discard(api);    
              } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      };
      
      
      /**
       * Delete File from Google Drive
       * @param result
       */
      public void DeleteFromGoogleDrive(DriveContentsResult result){
          Query query = new Query.Builder()
              .addFilter(Filters.eq(SearchableField.TITLE, GOOGLE_DRIVE_FILE_NAME))
              .build();
          Drive.DriveApi.query(api, query)
                  .setResultCallback(new ResultCallback<MetadataBufferResult>() {
      
              @Override
              public void onResult(MetadataBufferResult result) {
                  try{
                      Metadata metadata = result.getMetadataBuffer().get(0);
                      /*String a = metadata.getTitle();
                      String b = metadata.getDescription();
                      long c = metadata.getFileSize();*/
                      DriveResource driveResource = metadata.getDriveId().asDriveResource();
                       if (metadata.isTrashable()) {
                           if (metadata.isTrashed()) {
                               driveResource.untrash(api).setResultCallback(trashStatusCallback);
                           } else {
                               driveResource.trash(api).setResultCallback(trashStatusCallback);
                           }
                       } else {
                           Log.d(TAG, "Error trying delete");
                       }
                  }catch(Exception e){
                      Log.d(TAG, "Error: metadata doesn't exist");
                  }
      
              }
          });
      }
      
      /**
       * Callback when call to trash or untrash is complete.
       */
      private final ResultCallback<Status> trashStatusCallback =
              new ResultCallback<Status>() {
                  @Override
                  public void onResult(Status status) {
                      if (!status.isSuccess()) {
                          Log.e(TAG, "Error trying delete: " + status.getStatusMessage());
                          return;
                      }else{
                          Log.e(TAG, "Deleted: " + status.getStatusMessage());
                      }
                  }
              };
      
      
      
      
      private File getDbPath() {
          return this.getDatabasePath(DATABASE_NAME);
      }
      
      
      @Override
      public void onConnectionSuspended(int cause) {
          // TODO Auto-generated method stub
          Log.v(TAG, "Connection suspended");
      
      }
      
      
      @Override
      public void onStart() {
          super.onStart();
          if(!mResolvingError) {
              api.connect(); // Connect the client to Google Drive
          }
      }
      
      @Override
      public void onStop() {
          super.onStop();
          api.disconnect(); // Disconnect the client from Google Drive
      }
      
      @Override
      public void onConnectionFailed(ConnectionResult result) {
          Log.v(TAG, "Connection failed");
          if(mResolvingError) { // If already in resolution state, just return.
              return;
          } else if(result.hasResolution()) { // Error can be resolved by starting an intent with user interaction
              mResolvingError = true;
              try {
                  result.startResolutionForResult(this, DIALOG_ERROR_CODE);
              } catch (SendIntentException e) {
                  e.printStackTrace();
              }
          } else { // Error cannot be resolved. Display Error Dialog stating the reason if possible.
              Toast.makeText(this, "Error: Connection failed", Toast.LENGTH_SHORT).show();
          }
      }
      
      
      
      @Override
      public void onConnected(Bundle connectionHint) {
          Log.v(TAG, "Connected successfully");
      
          /* Connection to Google Drive established. Now request for Contents instance, which can be used to provide file contents.
             The callback is registered for the same. */
          Drive.DriveApi.newDriveContents(api).setResultCallback(contentsCallback);
      }
      
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          if(requestCode == DIALOG_ERROR_CODE) {
              mResolvingError = false;
              if(resultCode == RESULT_OK) { // Error was resolved, now connect to the client if not done so.
                  if(!api.isConnecting() && !api.isConnected()) {
                      api.connect();
                  }
              }
          }
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2015-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 1970-01-01
        相关资源
        最近更新 更多