Uploading video to Google Drive programmatically (Android API)

前端 未结 5 873
小蘑菇
小蘑菇 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:51

    If you want to upload any file to Google Drive, then use the below code with Synchronization task, it will upload your file to Drive.

    AsyncTask task = new AsyncTask() 
                    {
                @Override
                protected String doInBackground(Void... params) 
                {
                   String file_type="video/mp4"; //write your file type
                   File body = new File();                   
                   File FileRtr = null;
                   body.setTitle(myfile.getName());
                   body.setMimeType(file_type);
                   body.setParents(Arrays.asList(new ParentReference().setId(LocationID))); //LocationID means the path in the drive e where you want to upload it
                try 
                {
                  FileContent mediaContent = new FileContent(file_type, myfile);
                  FileRtr = mService.files().insert(body, mediaContent).execute();
    
                  if ( FileRtr != null) 
                  {
                    System.out.println("File uploaded: " +  FileRtr.getTitle());
    
                  }
                  }
                    catch (IOException e) 
                    {
                     System.out.println("An error occurred: " + e.getMessage());
                    }
                    return null;
    
                } 
                protected void onPostExecute(String token) 
                {
                 Toast.makeText(mContext, "Uploaded Successfuly",Toast.LENGTH_LONG).show();
                } 
                };
            task.execute();     
    

提交回复
热议问题