如何在android中下载谷歌驱动器文件 [英] How to download google drive file in android

查看:25
本文介绍了如何在android中下载谷歌驱动器文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从谷歌驱动器下载文件.为此,我在 Google Drive SDk 中实现并使用了以下方法.

I wanted to download file from google drive. For this I have implemented in Google Drive SDk and used the following method.

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case REQUEST_CODE_OPENER:
            if (resultCode == RESULT_OK) {
                DriveId driveId = (DriveId) data.getParcelableExtra(
                        OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);


            }
            finish();
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

我也尝试使用输出流,但无法将数据保存到文件中.

I also tried using output stream but not able to save data to a file.

我已经尝试过搜索,但找不到任何有用的链接来指导我如何下载和存储文件.

I have tried searching around this, but couldn't find any useful link which can guide me how to download and store file.

推荐答案

IMO,您应该阅读以下一些有用的链接:

IMO, you should read some useful links below:

那么,请参考下面的片段,当然在获取输入流时,你可以将其保存到设备中的文件中,而不是打印到 Logcat 中.

Then, please refer to the following snippets, of course when getting the input stream, you can save it to a file in your device instead of printing to Logcat.

public class GoogleDriveActivity extends AppCompatActivity
        implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {           
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mProgressBar.setMax(100);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(Drive.API)
                    .addScope(Drive.SCOPE_FILE)  
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() {
        if (mGoogleApiClient != null) {
            mGoogleApiClient.disconnect();
        }
        super.onPause();
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        if (requestCode == RC_OPENER && resultCode == RESULT_OK) {
            mSelectedFileDriveId = data.getParcelableExtra(
                    OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult result) {
        // Called whenever the API client fails to connect.
        // Do something...
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {        
        // If there is a selected file, open its contents.
        if (mSelectedFileDriveId != null) {
            open();
            return;
        }

        // Let the user pick a file...
        IntentSender intentSender = Drive.DriveApi
                .newOpenFileActivityBuilder()
                .setMimeType(new String[]{"video/mp4", "image/jpeg", "text/plain"})
                .build(mGoogleApiClient);
        try {
            startIntentSenderForResult(intentSender, RC_OPENER, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            Log.e(TAG, "Unable to send intent", e);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
    }

    private void open() {        
        mProgressBar.setProgress(0);
        DriveFile.DownloadProgressListener listener = new DriveFile.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);
            }
        };
        DriveFile driveFile = mSelectedFileDriveId.asDriveFile();
        driveFile.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, listener)
                .setResultCallback(driveContentsCallback);
        mSelectedFileDriveId = null;
    }

    private final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
            new ResultCallback<DriveApi.DriveContentsResult>() {
                @Override
                public void onResult(@NonNull DriveApi.DriveContentsResult result) {
                    if (!result.getStatus().isSuccess()) {
                        Log.w(TAG, "Error while opening the file contents");
                        return;
                    }
                    Log.i(TAG, "File contents opened");
                    
                    // Read from the input stream an print to LOGCAT
                    DriveContents driveContents = result.getDriveContents();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(driveContents.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line;
                    try {
                        while ((line = reader.readLine()) != null) {
                            builder.append(line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String contentsAsString = builder.toString();
                    Log.i(TAG, contentsAsString);

                    // Close file contents
                    driveContents.discard(mGoogleApiClient);
                }
            };
}

这篇关于如何在android中下载谷歌驱动器文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆