2016-04-18 14 views
1

Ich verwende das Laufwerk api, um eine Datenbankdatei im versteckten App-Ordner auf Google Drive zu erstellen. Die Datenbankdatei heißt notes.db. Ich konnte die Datenbankdatei erfolgreich auf Google Drive hochladen, aber ich habe keine Ahnung, wie ich sie auf das Gerät des Benutzers zurückladen kann. Das ist es, was ich versuche zu tun. Meine App erstellt auf dem Gerät des Benutzers einen Ordner namens "School Binder". In diesem Ordner befindet sich ein weiterer Ordner mit dem Namen Notiz-Backups. Hier sichern Sie die Datenbank. Das Verzeichnis istDownload Datenbank Datei Für Google Drive API Android

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

Google-Laufwerk diese Datei und lädt sie auf den versteckten Ordner app nimmt. Jetzt möchte ich diese notes.db Datei in diesem App-Ordner auf Google Drive und lade es in dieses Verzeichnis auf dem Telefon herunterladen.

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

Wie mache ich das? Vielen Dank. Hier ist mein Code die Datenbank für das Hochladen dies zu fahren funktioniert richtig

 // 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;} 

Wie kann ich das ändern, um es funktioniert Daten in eine Datei von Google herunterzuladen fahren

+0

Sie die Dateien in der App-Ordner auflisten können, finden Sie die Datei in einem offen interessiert sind. Schaue hier: https://developers.google.com/drive/android/folders – Anatoli

Antwort

0

ich es herausgefunden dies mein Code ist eine Datenbank zurück, um das Telefon

 //<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> 
0

In Lifecycle of a Drive file, Drive Android-API können Sie Ihre App-Zugriffsdateien, selbst wenn das Gerät offline ist. Zur Unterstützung von Offline-Fällen implementiert die API eine Synchronisierungs-Engine, die im Hintergrund für Upstream- und Downstream-Änderungen ausgeführt wird, wenn Netzwerkzugriff verfügbar ist und Konflikte gelöst werden können. Führen Sie eine erste Download-Anforderung aus, wenn die Datei noch nicht mit dem lokalen Kontext synchronisiert ist, der Benutzer die Datei jedoch öffnen möchte. Die API verarbeitet dies automatisch, wenn eine Datei angefordert wird.

In , Sie eine autorisierte HTTP-GET-Anfrage an die Ressourcen-URL der Datei und den Abfrageparameter alt=media. Beachten Sie jedoch, dass beim Herunterladen der Datei der Benutzer mindestens Lesezugriff benötigt.

Beispiel HTTP-Request:

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media Berechtigung: Bearer ya29.AHESVbXTUv5mHMo3RYfmS1YJonjzzdTOFZwvyOAUVhrs

Für die Codierung Teil dieses SO post könnte auch hilfreich sein.

+0

check edit oben bitte – Jordan

0

hier ist eine komplette Klasse hochladen eine interne Datenbank, laden Sie es und löschen Sie es von Google Drive redownload.

Nur Funktionen asynchron aufrufen und Benutzer eine Fortschrittsanzeige anzeigen. DownloadFromGoogleDrive Funktion speichert die Datenbank im internen Datenbankordner in der App mit dem Namen "database2"

Ich hoffe, es ist hilfreich.

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

}

Verwandte Themen