2016-09-26 4 views
1

私は機能があり、正常に動作しています!しかしそのパスでドライブID?ドライブIDが削除または破棄された場合はどうなりますか?DriveIdなしでAppフォルダgoogleドライブからファイルを読み取る方法は?

は、ドライブIDを見つける方法ですか?

私は共有好み

から私のドライブIDを逃した場合、私は=======

  1. ユーザーに再参加私のアプリや変更デバイス
  2. ようなシナリオで困難に直面=============================== 私作業ファンクション

    readFromGooDrive(DriveId mDriveId) { 
         byte[] buf = null; 
         if (getGoogleApiClient() != null && getGoogleApiClient().isConnected()) try { 
         DriveFile df = Drive.DriveApi.getFile(getGoogleApiClient(), mDriveId); 
         df.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null) 
          .setResultCallback(new ResultCallback<DriveContentsResult>() { 
          @Override 
          public void onResult(DriveContentsResult driveContentsResult) { 
          if ((driveContentsResult != null) && driveContentsResult.getStatus().isSuccess()) { 
           DriveContents contents = driveContentsResult.getDriveContents(); 
           BufferedInputStream bis = new BufferedInputStream(contents.getInputStream()); 
           byte[] buffer = new byte[1024]; 
           int bytesread = 0; 
           FileOutputStream outStream; 
           try 
           { 
            String databasePath = getBaseContext().getDatabasePath("my database").getPath(); 
            outStream = new FileOutputStream(databasePath); 
            while((bytesread = bis.read(buffer)) != -1) 
            { 
             outStream.write(buffer,0,bytesread); 
            } 
    
            outStream.flush(); 
            bis.close(); 
            outStream.close(); 
           } 
           catch (FileNotFoundException e) 
           { 
            Log.i(TAG,e.getMessage()); 
           } 
           catch (IOException e) 
           { 
            Log.i(TAG,e.getMessage()); 
           } 
           finally { 
            Toast.makeText(getBaseContext(),"Data from Google Drive restored successfully." , Toast.LENGTH_LONG).show(); 
           } 
           contents.discard(getGoogleApiClient()); 
          } 
          } 
         }); 
         } catch (Exception e) { e.printStackTrace(); } 
        } 
    
+0

その作業完璧仲間を必要とし、このリンクを読んで:私はdriveID – dipali

+0

を使用する必要はありませんが、あなたができると思うhttps://developers.google.com/drive/android/auth を'DriveId'が削除されるという例を挙げてください。 – Teyam

+0

mydbファイルをappフォルダのドライブにアップロードしています。今、私はアプリをアンインストールしてからもう一度インストールして、アプリのフォルダの内容を取得しようとしていますが、DriveIdなしでは得られません。 – Harish

答えて

0

あなただけ..ファイルをコピーする

private static final String TAG = "EditContentsActivity"; 

@Override 
public void onConnected(Bundle connectionHint) { 
    super.onConnected(connectionHint); 

    Query query = new Query.Builder() 
      .addFilter(Filters.contains(SearchableField.TITLE, "New file2.txt")) 
      .build(); 
    Drive.DriveApi.query(getGoogleApiClient(), query) 
      .setResultCallback(metadataCallback); 


} 

public class EditContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Void, Boolean> { 

    public EditContentsAsyncTask(Context context) { 
     super(context); 
    } 

    @Override 
    protected Boolean doInBackgroundConnected(DriveFile... args) { 
     DriveFile file = args[0]; 
     try { 
      DriveContentsResult driveContentsResult = file.open(
        getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await(); 
      if (!driveContentsResult.getStatus().isSuccess()) { 
       return false; 
      } 
      DriveContents driveContents = driveContentsResult.getDriveContents(); 
      OutputStream outputStream = driveContents.getOutputStream(); 
      outputStream.write("New file2.txt".getBytes()); 
      com.google.android.gms.common.api.Status status = 
        driveContents.commit(getGoogleApiClient(), null).await(); 
      return status.getStatus().isSuccess(); 
     } catch (IOException e) { 
      Log.e(TAG, "IOException while appending to the output stream", e); 
     } 
     return false; 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     if (!result) { 
      showMessage("Error while editing contents"); 
      return; 
     } 
     showMessage("Successfully edited contents"); 
    } 
} 

final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback = 
     new ResultCallback<DriveApi.MetadataBufferResult>() { 
      @Override 
      public void onResult(DriveApi.MetadataBufferResult result) { 
       if (!result.getStatus().isSuccess()) { 
        showMessage("Problem while retrieving results"); 
        return; 
       } 
       MetadataBuffer mdb = null; 
       try { 
        mdb = result.getMetadataBuffer(); 
        for (Metadata md : mdb) { 
         if (md == null || !md.isDataValid() || md.isTrashed()) continue; 
         // collect files 
         DriveId driveId = md.getDriveId(); 
         String dId = driveId.encodeToString(); 
         Log.i("checking",""+dId); 
         String mime = md.getMimeType(); 
         String name=md.getTitle(); 
         if(name.equals("New file2.txt")){ 
          Log.i("checking2",""+dId); 
          Log.i("checking3",""+driveId.getResourceId()); 
          Drive.DriveApi.fetchDriveId(getGoogleApiClient(), driveId.getResourceId()) 
            .setResultCallback(idCallback); 
          break; 
         } 
         //..... 
        } 
       } finally { if (mdb != null) mdb.close(); } 

      } 
     }; 

final ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() { 
    @Override 
    public void onResult(DriveIdResult result) { 
     if (!result.getStatus().isSuccess()) { 
      showMessage("Cannot find DriveId. Are you authorized to view this file?"); 
      return; 
     } 
     DriveId driveId = result.getDriveId(); 
     DriveFile file = driveId.asDriveFile(); 
     new EditContentsAsyncTask(EditContentActivity.this).execute(file); 
    } 
}; 
関連する問題