简体   繁体   中英

Download file from drive to upload into another drive using Google Drive API in Android Studio

So, In my app I need to get a file from the user's drive and upload it to mine.

I've searched and it seems like the only way to do this is by downloading the file from the user's drive and then uploading it to mine.

I already have all the OAuth2 settled and I'm able to create, get and list files.

But my problem is that according to google's documentation, I'm suposed to download the file to a ByteArrayOutputStream declared as an OutputStream, but when I upload the file, I'm supposed to upload a java.io file

Does anyone knows how I can get the outputstream from the download method and turn it into a file in order to send to the upload method?

This is my code so far:

The code to download the file (I got it from google)

 public Task<java.io.File> downloadFile(PostFileHolder postFileHolder) {
        return Tasks.call(mExecutor, () -> {
            // Retrieve the metadata as a File object.

            Log.i("download file", "chegou");

            OutputStream outputStream = new ByteArrayOutputStream();
            mDriveService.files().export(postFileHolder.getGoogleId(), "application/pdf")
                    .executeMediaAndDownloadTo(outputStream);
          // I need to use the export method because I will have some docs and slides files

            return null;
        });
    }

And this is the code to upload files:

public Task<File> uploadFileWithMetadata(java.io.File javaFile, String mimeType, boolean isSlide,  @Nullable final String folderId) {
        return Tasks.call(mExecutor, () -> {

            Log.i("upload file", "chegou" );

            String convertTo;
            if(isSlide){
                convertTo = TYPE_GOOGLE_SLIDES;
            }
            else{
                convertTo = TYPE_GOOGLE_DOCS;
            }

            List<String> folder;
            if (folderId == null) {
                folder = Collections.singletonList("root");
            } else {

                folder = Collections.singletonList(folderId);
            }
            File metadata = new File()
            .setParents(folder)
            .setName(javaFile.getName())
            .setMimeType(convertTo);

            FileContent mediaContent = new FileContent(convertTo, javaFile);

            File uploadedFile = mDriveService.files().create(metadata, mediaContent)
                    .setFields("id,name,size,createdTime,modifiedTime,starred,thumbnailLink,mimeType")
                    .execute();

            Log.i("File ID: " , uploadedFile.getId());

            return uploadedFile;
        });
    }

Thanks!

You can simply use following minimal code to create a file. And use this file later on to upload.

val outputStream = ByteArrayOutputStream() // Your 
val byteData = bos.toByteArray()


val mainFile = File("Path with filename")
//write the bytes in file
val fos = FileOutputStream(mainFile)
fos.write(bitmapdata)
fos.flush()
fos.close()

Since ur outputStream was initialized as ByteArrayOutputStream so you can simply use cast method eg ((ByteArrayOutputStream) outputStream).toByteArray()

Regarding path, You can save file anywhere in your internal file directory or external storage as temporary file. I would recommend you to go through Data and File Storage Overview and this tutorial for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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