Uploading video to Google Drive programmatically (Android API)

前端 未结 5 888
小蘑菇
小蘑菇 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 06:57

    So this my complete code how I achieved uploading a video. Steps:

    • Fetch video file from uri(as in my case).
    • Get the byte array from bytearray outputstream as mentioned in the code
    • write the byte array to the ouputStream
    • the api will upload the file in the background

    public class UploadVideo extends AppCompatActivity {

    DriveClient mDriveClient;
    DriveResourceClient mDriveResourceClient;
    GoogleSignInAccount googleSignInAccount;
    String TAG = "Drive";
    private final int REQUEST_CODE_CREATOR = 2013;
    Task createContentsTask;
    String uri;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_video);
        //Fetching uri or path from previous activity.
        uri = getIntent().getStringExtra("uriVideo");
        //Get previously signed in account.
        googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this);
        if (googleSignInAccount != null) {
            mDriveClient = Drive.getDriveClient(getApplicationContext(), googleSignInAccount);
            mDriveResourceClient =
                    Drive.getDriveResourceClient(getApplicationContext(), googleSignInAccount);
        }
        else Toast.makeText(this, "Login again and retry", Toast.LENGTH_SHORT).show();
        createContentsTask = mDriveResourceClient.createContents();
        findViewById(R.id.uploadVideo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    createFile();
            }
        });
    }
    
    private void createFile() {
        // [START create_file]
        final Task rootFolderTask = mDriveResourceClient.getRootFolder();
        final Task createContentsTask = mDriveResourceClient.createContents();
        Tasks.whenAll(rootFolderTask, createContentsTask)
                .continueWithTask(new Continuation>() {
                    @Override
                    public Task then(@NonNull Task task) throws Exception {
                        DriveFolder parent = rootFolderTask.getResult();
                        DriveContents contents = createContentsTask.getResult();
                        File file = new File(uri);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] buf = new byte[1024];
                        FileInputStream fis = new FileInputStream(file);
                        for (int readNum; (readNum = fis.read(buf)) != -1;) {
                            baos.write(buf, 0, readNum);
                        }
                        OutputStream outputStream = contents.getOutputStream();
                        outputStream.write(baos.toByteArray());
    
                        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                .setTitle("MyVideo.mp4") // Provide you video name here
                                .setMimeType("video/mp4") // Provide you video type here
                                .build();
    
                        return mDriveResourceClient.createFile(parent, changeSet, contents);
                    }
                })
                .addOnSuccessListener(this,
                        new OnSuccessListener() {
                            @Override
                            public void onSuccess(DriveFile driveFile) {
                                Toast.makeText(Upload.this, "Upload Started", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e(TAG, "Unable to create file", e);
                        finish();
                    }
                });
        // [END create_file]
    }
    

    }

提交回复
热议问题