Non riesco a darti il codice esatto che fa ciò che ti serve, ma puoi provare a modificare l'API di Android di Google Drive (GDAA) the code I use for testing. Crea cartelle e carica file su Google Drive. Spetta a te se scegli il sapore REST o GDAA, ognuno ha i suoi vantaggi specifici.
Questo copre solo metà della domanda, però. La selezione e l'enumerazione dei file sul tuo dispositivo Android dovrebbero essere trattati altrove.
UPDATE: (per il commento di Frank sotto)
L'esempio che ho citato sopra darebbe una soluzione completa da zero, ma cerchiamo di affrontare i punti della sua domanda potrei decifrare:
L'ostacolo 'qualche errore' è un metodo che restituisce l'oggetto GoogleApiClient inizializzato prima della sequenza di codice. Sarebbe simile:
GoogleApiClient mGAC = new GoogleApiClient.Builder(appContext)
.addApi(Drive.API).addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(callerContext)
.addOnConnectionFailedListener(callerContext)
.build();
Se avete questo sgomberato, supponiamo che la cartellaè rappresentato da un oggetto java.io.File. Ecco il codice che:
1/enumera i file nella cartella locale si
2/imposta il nome, il contenuto e MIME tipo di ciascun file (utilizzando jpeg per semplicità qui).
3/uploads ogni file nella cartella principale di Google Drive
(il creare() metodo deve eseguire filo off-UI)
// enumerating files in a folder, uploading to Google Drive
java.io.File folder = ...;
for (java.io.File file : folder.listFiles()) {
create("root", file.getName(), "image/jpeg", file2Bytes(file))
}
/******************************************************
* 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 buf file contents (optional, if null, create folder)
* @return file id/null on fail
*/
static String create(String prnId, String titl, String mime, byte[] buf) {
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 (buf != null) { // create file
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 = bytes2Cont(r1.getDriveContents(), buf).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) { /* error handling*/ }
} else {
meta = new MetadataChangeSet.Builder().setTitle(titl).setMimeType("application/vnd.google-apps.folder").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) { /* error handling*/ }
return dId == null ? null : dId.encodeToString();
}
//-----------------------------
static byte[] file2Bytes(File file) {
if (file != null) try {
return is2Bytes(new FileInputStream(file));
} catch (Exception e) {}
return null;
}
//----------------------------
static byte[] is2Bytes(InputStream is) {
byte[] buf = null;
BufferedInputStream bufIS = null;
if (is != null) try {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
bufIS = new BufferedInputStream(is);
buf = new byte[2048];
int cnt;
while ((cnt = bufIS.read(buf)) >= 0) {
byteBuffer.write(buf, 0, cnt);
}
buf = byteBuffer.size() > 0 ? byteBuffer.toByteArray() : null;
} catch (Exception e) {}
finally {
try {
if (bufIS != null) bufIS.close();
} catch (Exception e) {}
}
return buf;
}
//--------------------------
private static DriveContents bytes2Cont(DriveContents driveContents, byte[] buf) {
OutputStream os = driveContents.getOutputStream();
try { os.write(buf);
} catch (IOException e) {/*error handling*/}
finally {
try { os.close();
} catch (Exception e) {/*error handling*/}
}
return driveContents;
}
Aghi a dire il codice qui è presa direttamente dalla GDAA wrapper here (menzionato all'inizio), quindi se hai bisogno di risolvere qualsiasi riferimento devi cercare il codice lì.
Good Luck
"mostra qualche errore": dettagli per favore! – Henry
qui mostra errore .build (getGoogleApiClient()); sta mostrando che getGoogleApiClient non è disponibile e non sono in grado di creare oggetti per GoogleApiClient da passare in build – Hanuman