Uploading video to Google Drive programmatically (Android API)

前端 未结 5 889
小蘑菇
小蘑菇 2020-12-09 06:40

I have followed the Drive API guide (https://developer.android.com/google/play-services/drive.html) and my app now uploads photos smoothly, but I am now trying to upload vid

5条回答
  •  不知归路
    2020-12-09 07:00

     private void saveFiletoDrive(final File file, final String mime) {
            Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(
                    new ResultCallback() {
    
                        @Override
                        public void onResult(DriveContentsResult result) {
    
                            if (!result.getStatus().isSuccess()) {
                                Log.i(TAG, "Failed to create new contents.");
                                return;
                            }
                            Log.i(TAG, "Connection successful, creating new contents...");
                            OutputStream outputStream = result.getDriveContents()
                                    .getOutputStream();
                            FileInputStream fis;
                            try {
                                fis = new FileInputStream(file.getPath());
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                byte[] buf = new byte[1024];
                                int n;
                                while (-1 != (n = fis.read(buf)))
                                    baos.write(buf, 0, n);
                                byte[] photoBytes = baos.toByteArray();
                                outputStream.write(photoBytes);
    
                                outputStream.close();
                                outputStream = null;
                                fis.close();
                                fis = null;
    
                            } catch (FileNotFoundException e) {
                                Log.w(TAG, "FileNotFoundException: " + e.getMessage());
                            } catch (IOException e1) {
                                Log.w(TAG, "Unable to write file contents." + e1.getMessage());
                            }
    
                            String title = file.getName();
                            MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                                    .setMimeType(mime).setTitle(title).build();
    
                            if (mime.equals(MIME_PHOTO)) {
                                if (VERBOSE)
                                    Log.i(TAG, "Creating new photo on Drive (" + title
                                            + ")");
                                Drive.DriveApi.getFolder(mGoogleApiClient,
                                        mPicFolderDriveId).createFile(mGoogleApiClient,
                                        metadataChangeSet,
                                        result.getDriveContents());
                            } else if (mime.equals(MIME_VIDEO)) {
                                Log.i(TAG, "Creating new video on Drive (" + title
                                        + ")");
                                Drive.DriveApi.getFolder(mGoogleApiClient,
                                        mVidFolderDriveId).createFile(mGoogleApiClient,
                                        metadataChangeSet,
                                        result.getDriveContents());
                            }
    
                            if (file.delete()) {
                                if (VERBOSE)
                                    Log.d(TAG, "Deleted " + file.getName() + " from sdcard");
                            } else {
                                Log.w(TAG, "Failed to `enter code here`delete " + file.getName() + " from sdcard");
                            }
                        }
                    });
        }
    

提交回复
热议问题