将onShowFileChooser与Google Drive API一起使用

时间:2017-02-08 08:39:18

标签: android webview google-drive-api google-drive-android-api

是否可以使用google drive API实现文件选择器?

我已尝试过此代码,但我不知道接下来要做什么。

这是我的网络Chrome客户端:

webView.setWebChromeClient(new WebChromeClient() {
            public boolean onShowFileChooser(
                    WebView webView, ValueCallback<Uri[]> filePathCallback,
                    WebChromeClient.FileChooserParams fileChooserParams) {

                Log.d(TAG, "CHOOSE FILE SHOW");
                mFilePathCallback = filePathCallback;

                IntentSender intentSender = Drive.DriveApi
                        .newOpenFileActivityBuilder()
                        .build(getGoogleApiClient());
                try {
                    startIntentSenderForResult(
                            intentSender, INPUT_FILE_REQUEST_CODE, null, 0, 0, 0);
                } catch (IntentSender.SendIntentException e) {
                    Log.w(TAG, "Unable to send intent", e);
                }

                return true;
            }
        });

这是活动结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
        super.onActivityResult(requestCode, resultCode, data);
        return;
    }

    Uri[] results;

    // Check that the response is a good one
    if (resultCode == Activity.RESULT_OK) {
        DriveId driveId = data.getParcelableExtra(
                OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        com.google.api.services.drive.Drive mService = new com.google.api.services.drive.Drive.Builder(
                transport, jsonFactory, mCredential)
                .setApplicationName("Drive API Android Quickstart")
                .build();
        try {
            File file = mService.files().get(driveId.getResourceId()).execute();
            results = new Uri[]{Uri.parse(file.getDownloadUrl())};
            mFilePathCallback.onReceiveValue(results);
        } catch (IOException e) {
            e.printStackTrace();
        }

        mFilePathCallback = null;
    }
}

接下来的问题是,如何从driveId获取文件?我应该使用API​​吗?

0 个答案:

没有答案