以下程序将显示google驱动器中的“与您共享的文件”。我在listView中显示所有文件,这里我想在用户单击列表项时下载google驱动器文件,并且我已经编写了代码来获取文件的驱动器ID。
这是我在以下代码中所做的。
每当用户使用google drive android api点击列表项时,请帮我从google驱动器下载文件。
public class MainActivity extends BaseDemoActivity{
private static final String TAG = "MainActivity";
private ListView mResultsListView;
private ResultsAdapter mResultsAdapter;
private ProgressBar mProgressBar;
private DriveId mSelectedFileDriveId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResultsListView = (ListView) findViewById(R.id.listViewSamples);
mResultsAdapter = new ResultsAdapter(this);
mResultsListView.setAdapter(mResultsAdapter);
mResultsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (mSelectedFileDriveId != null) {
open();
return;
}
}
});
}
private void open() {
showMessage("Select files drive id = "+mSelectedFileDriveId);
// Reset progress dialog back to zero as we're
// initiating an opening request.
// 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);
}
};
Drive.DriveApi.getFile(getGoogleApiClient(), mSelectedFileDriveId)
.open(getGoogleApiClient(), DriveFile.MODE_READ_WRITE, listener)
.setResultCallback(driveContentsCallback);
mSelectedFileDriveId = null;
}
private ResultCallback<DriveContentsResult> driveContentsCallback =
new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
showMessage("Error while opening the file contents");
return;
}
showMessage("File contents opened");
}
};
/**
* 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);
Drive.DriveApi.requestSync(getGoogleApiClient())
.setResultCallback(statusCallback);
showMessage("Connecting ...");
}
final private ResultCallback<Status> statusCallback =
new ResultCallback<Status>() {
@Override
public void onResult(Status arg0) {
// TODO Auto-generated method stub
showMessage("Fetching...");
if(arg0.getStatus().isSuccess()){
Query query = new Query.Builder()
.addFilter(Filters.sharedWithMe())
.build();
Drive.DriveApi.query(getGoogleApiClient(), query)
.setResultCallback(metadataCallback);
}else{
showMessage("Error = "+arg0);
}
}
};
final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback =
new ResultCallback<DriveApi.MetadataBufferResult>() {
@Override
public void onResult(DriveApi.MetadataBufferResult result) {
// TODO Auto-generated method stub
showMessage("onResult ...");
if (!result.getStatus().isSuccess()) {
showMessage("Problem while retrieving results");
return;
}
mResultsAdapter.clear();
mResultsAdapter.append(result.getMetadataBuffer());
MetadataBuffer buffer = result.getMetadataBuffer();
Metadata meta = buffer.get(0);
Log.v(TAG,"WEB CONTENT LINK = "+meta.getWebContentLink());
mSelectedFileDriveId = (DriveId) meta.getDriveId();
showMessage("Count = "+mResultsAdapter.getCount());
}
};
}
答案 0 :(得分:0)
由于API中没有直接下载文件的特定方法,因此有两种解决方法可以实现此目的:
a)通过调用getFile()方法创建DriveFile对象。然后,使用java.io.InputStream库打开文件,并将此流转换为本地文件。有关此资源的更多信息:https://developers.google.com/drive/android/files
b)使用元数据WebContentLink,通过浏览器上的意图或通过WebView访问该URL。您可能需要再次进行身份验证。在浏览器上打开文件应该提示下载请求。
答案 1 :(得分:0)
在以下位置查看Google Drive Android API示例:
https://github.com/googledrive/android-demos
RetrieveContentsActivity代码向您展示如何将文件内容导入InputStreamReader。
要运行该示例,您必须在
中注册一个新应用https://console.developers.google.com/
启用Google Drive API。
要使用此API,您需要在Credentials / OAuth(在Google控制台中)中创建新的客户端ID。
您需要使用keytool生成SHA1密钥并将其粘贴到那里。
Windows示例:
C:\Program Files\Java\jre1.8.0_40\bin>keytool -exportcert -alias androiddebugkey
-keystore C:\Users\yourusername\.android\debug.keystore -list -v