2015-01-31 8 views
10

Chcę wysłać wiele obrazów, które są obecne w mojej pamięci wewnętrznej, a kiedy wybiorę ten folder, chcę przesłać ten folder na dysk google. Próbowałem tego napędu google api dla Android https://developers.google.com/drive/android/create-file i Użyłem poniższy kod, ale to pokazuje jakiś błąd w getGoogleApiClientJak wysyłać wiele obrazów, które są obecne w folderze na dysk Google w programie Android programowo?

kod jest

ResultCallback<DriveContentsResult> contentsCallback = new 
     ResultCallback<DriveContentsResult>() { 
    @Override 
    public void onResult(DriveContentsResult result) { 
     if (!result.getStatus().isSuccess()) { 
      // Handle error 
      return; 
     } 

     MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() 
       .setMimeType("text/html").build(); 
     IntentSender intentSender = Drive.DriveApi 
       .newCreateFileActivityBuilder() 
       .setInitialMetadata(metadataChangeSet) 
       .setInitialDriveContents(result.getDriveContents()) 
       .build(getGoogleApiClient()); 
     try { 
      startIntentSenderForResult(intentSender, 1, null, 0, 0, 0); 
     } catch (SendIntentException e) { 
      // Handle the exception 
     } 
    } 
} 

jest jakieś podejście do wysyłania obrazów do napędu lub gmail ?

+0

"pokazuje jakiś błąd": szczegóły proszę! – Henry

+0

tutaj wyświetla błąd .build (getGoogleApiClient()); pokazuje, że getGoogleApiClient nie jest dostępny i nie mogę utworzyć obiektu dla GoogleApiClient, aby przekazać kompilację – Hanuman

Odpowiedz

3

Nie mogę podać dokładnego kodu, który spełnia Twoje potrzeby, ale możesz spróbować zmodyfikować interfejs Google Drive Android API (GDAA) na the code I use for testing. Tworzy foldery i przesyła pliki na Dysk Google. To od Ciebie zależy, czy wybierzesz smak REST, czy GDAA, każdy ma swoje szczególne zalety.

To jednak tylko połowa twojego pytania. Wybór i wyliczanie plików na urządzeniu z systemem Android powinny być pokryte w innym miejscu.

UPDATE: (za komentarzem Franka poniżej)

Przykład wspomniałem powyżej nie daje pełnego rozwiązania od podstaw, ale niech zająć punkty Twojego pytania mogę rozszyfrować:

Przeszkoda "jakiś błąd" jest metodą, która zwraca obiekt GoogleApiClient zainicjalizowany przed twoją sekwencją kodu. Będzie wyglądać następująco:

GoogleApiClient mGAC = new GoogleApiClient.Builder(appContext) 
    .addApi(Drive.API).addScope(Drive.SCOPE_FILE) 
    .addConnectionCallbacks(callerContext) 
    .addOnConnectionFailedListener(callerContext) 
    .build(); 

Jeśli to wyczyszczone, załóżmy folderjest reprezentowany przez obiekt java.io.File. Oto kod, który:

1/wymienia pliki z lokalnym folderze
2/ustawia nazwę, zawartość i MIME typ każdego pliku (przy użyciu JPEG dla prostoty tutaj).
3/przesłane każdy plik do głównego folderu Dysk Google
(The create() metoda musi uruchomić wątek 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; 
} 

Igły powiedzieć kod tutaj jest pobierana bezpośrednio z GDAA wrapper here (wspomniałem na początku), więc jeśli musisz rozwiązać wszelkie odniesienia, musisz znaleźć tam kod.

Powodzenia

Powiązane problemy