简体   繁体   English

Android - Google Drive SDK - 打开文件

[英]Android - Google Drive SDK - Open file

I am used to opening my files in my apps using the next code:我习惯于使用以下代码在我的应用程序中打开文件:

public void openFile(@NonNull String uri) {
        checkNotNull(uri);
        File file = new File(uri);

        String dataType = null;
        if (ContentTypeUtils.isPdf(uri)) dataType = "application/pdf";
        else if (ContentTypeUtils.isImage(uri)) dataType = "image/*";

        if (file.exists() && dataType != null) {
            Intent target = new Intent(Intent.ACTION_VIEW);

            target.setDataAndType(Uri.fromFile(file), dataType);
            target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

            Intent intent = Intent.createChooser(target, "Open file");
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
                Log.e(TAG, "There is a problem when opening the file :(");
            }
        } else {
            Toast.makeText(getContext(), "Invalido", Toast.LENGTH_LONG).show();
        }
    }

I had always used static files so this was enough, but now I am using the Google Drive SDK for Android.我一直使用静态文件,所以这就足够了,但现在我使用的是适用于 Android 的 Google Drive SDK。 I possess the driveId of the file I want to open but the problem is I cannot find a clean way to open the file contents I obtain by doing this:我拥有要打开的文件的 driveId,但问题是我找不到一种干净的方法来打开通过执行此操作获得的文件内容:

Drive.DriveApi.fetchDriveId(mGoogleApiClient, documentFile.getDriveId())
            .setResultCallback(driveIdResult -> {
                PendingResult<DriveApi.DriveContentsResult> open =
                        driveIdResult.getDriveId().asDriveFile().open(
                        mGoogleApiClient,
                        DriveFile.MODE_READ_ONLY,
                        null);

                open.setResultCallback(result -> {
                    DriveContents contents = result.getDriveContents();
                    InputStream inputStream = contents.getInputStream();
                    // I know I can get the input stream, and use it to write a new file.

                });
            });

So the only thing that comes to my mind is creating a static route to create a file every time I have to open it, and erasing it every time I have to open a new file.所以我想到的唯一一件事就是创建一个静态路径来创建一个文件,每次我必须打开它,每次我必须打开一个新文件时删除它。

What I have understood up until now is that the Google Drive API for Android already saves an instance of the file so what I have in mind sounds unnecessary, I would like to know if there is a better way to achieve this.到目前为止,我所理解的是,Android 的 Google Drive API 已经保存了一个文件实例,所以我的想法听起来没有必要,我想知道是否有更好的方法来实现这一点。 Is there a way I can open the file and do something similar to what I do with the Intent.ACTION_VIEW in a cleaner way?有没有一种方法可以打开文件并以更清洁的方式执行与Intent.ACTION_VIEW类似的操作?

Thanks in advance.提前致谢。

Well since it seems this will not be answered I will post what I did.好吧,既然这似乎不会得到回答,我会发布我所做的。 All I did was create a temp file where I put my contents to be read.我所做的只是创建一个临时文件,在其中放置要读取的内容。 I still don't know if it was the best choice so this question will still be opened for a better answer.我仍然不知道这是否是最好的选择,所以这个问题仍然会被打开以获得更好的答案。

open.setResultCallback(result -> {
                    DriveContents contents = result.getDriveContents();
                    InputStream inputStream = contents.getInputStream();
                    writeTempFile(inputStream);
                });

And here the implementation of the `writeTempFile`:

    private synchronized File writeTempFile(@NonNull InputStream inputStream) {
        checkNotNull(inputStream);
        File filePath = new File(mActivity.getFilesDir(), "TempFiles");
        if (!filePath.exists()) filePath.mkdirs();
        File file = new File(filePath, TEMP_FILE);
        try {
            OutputStream outputStream = new FileOutputStream(file);
            IOUtils.copyLarge(inputStream, outputStream);
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return file;
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM