根据Google驱动器ID获取文件内容

时间:2014-10-05 17:39:16

标签: android google-drive-api

在我的项目中,我必须备份并恢复.db SQLite数据库到谷歌驱动器。我查看了谷歌文档和演示项目。我的DRIVE ID有问题。

那是我的客户:

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER) // required for App Folder sample
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

我已创建文件,谷歌显示:

public class CreateFileActivity extends BaseDemoActivity {

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    // create new contents resource
    Drive.DriveApi.newContents(getGoogleApiClient())
            .setResultCallback(contentsCallback);
}

final private ResultCallback<ContentsResult> contentsCallback = new
        ResultCallback<ContentsResult>() {
    @Override
    public void onResult(ContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }

        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle("testFile.txt")
                .setMimeType("text/plain")
                .setStarred(true).build();
        // create a file on root folder
        Drive.DriveApi.getRootFolder(getGoogleApiClient())
                .createFile(getGoogleApiClient(), changeSet, result.getContents())
                .setResultCallback(fileCallback);
    }
};

final private ResultCallback<DriveFileResult> fileCallback = new
        ResultCallback<DriveFileResult>() {
    @Override
    public void onResult(DriveFileResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create the file");
            return;
        }
        showMessage("Created a file: " + result.getDriveFile().getDriveId());
        Log.e("misha", ""+result.getDriveFile().getDriveId() );
    }
};
}

然后通过我的电影名称检索它的驱动器ID并将其打印到日志:

public class QueryFilesActivity extends BaseDemoActivity {

private ListView mResultsListView;
private ResultsAdapter mResultsAdapter;

@Override
protected void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.activity_listfiles);
    mResultsListView = (ListView) findViewById(R.id.listViewResults);
    mResultsAdapter = new ResultsAdapter(this);
    mResultsListView.setAdapter(mResultsAdapter);
}

/**
 * Clears the result buffer to avoid memory leaks as soon
 * as the activity is no longer visible by the user.
 */
@Override
protected void onStop() {
    super.onStop();
    mResultsAdapter.clear();
}

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    Query query = new Query.Builder()
            //.addFilter(Filters.eq(SearchableField.MIME_TYPE, "text/plain"))
        .addFilter(Filters.eq(SearchableField.TITLE, "testFile.txt"))
            .build();
    Drive.DriveApi.query(getGoogleApiClient(), query)
            .setResultCallback(metadataCallback);
}

final private ResultCallback<MetadataBufferResult> metadataCallback = new
        ResultCallback<MetadataBufferResult>() {
    @Override
    public void onResult(MetadataBufferResult result) {
        MetadataBuffer metaData = result.getMetadataBuffer();
        for (int i = 0 ; i <metaData.getCount(); i++){
            Log.e("misha", metaData.get(i).getDriveId().toString());
        }
        if (!result.getStatus().isSuccess()) {
            showMessage("Problem while retrieving results");
            return;
        }
        mResultsAdapter.clear();
        mResultsAdapter.append(result.getMetadataBuffer());
    }
};
}

所以,我没有\我的文件驱动器ID XXXXXXXXXXZiUUxXM0lIUWFhV0pFV0hwSFVqWXhXRzgY9AXXXXXXXXXX。我想这是正确的ID。现在当我想获取文件内容时。我收到以下错误

Drive.DriveApi.fetchDriveId(getGoogleApiClient(),EXISTING_FILE_ID)               .setResultCallback(idCallback);

  
    

找不到DriveId。您是否有权查看此文件?

  

我想获取文件的内容,我也想更改内容等等。但我坚持检索文件内容:(请帮助人

0 个答案:

没有答案