Last active
September 16, 2020 06:11
-
-
Save tolunayozturk/2293087571b1716b763c0ade29fe57bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void uploadFiles() { | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
if (mAccessToken == null) { | |
Log.d(TAG, "need to sign in first"); | |
return; | |
} | |
if (StringUtils.isNullOrEmpty(et_uploadFileName.getText().toString())) { | |
Log.d(TAG, "file name is required to upload"); | |
return; | |
} | |
String path = getExternalFilesDir(null).getAbsolutePath() | |
+ "/" + et_uploadFileName.getText(); | |
Log.d(TAG, "run: " + path); | |
java.io.File fileObj = new java.io.File(path); | |
if (!fileObj.exists()) { | |
Log.d(TAG, "file does not exists"); | |
return; | |
} | |
Drive drive = buildDrive(); | |
Map<String, String> appProperties = new HashMap<>(); | |
appProperties.put("appProperties", "property"); | |
String dirName = "Uploads"; | |
File file = new File(); | |
file.setFileName(dirName) | |
.setAppSettings(appProperties) | |
.setMimeType("application/vnd.huawei-apps.folder"); | |
directoryCreated = drive.files().create(file).execute(); | |
// Upload the file | |
File fileToUpload = new File() | |
.setFileName(fileObj.getName()) | |
.setMimeType(mimeType(fileObj)) | |
.setParentFolder(Collections.singletonList(directoryCreated.getId())); | |
Drive.Files.Create request = drive.files() | |
.create(fileToUpload, new FileContent(mimeType(fileObj), fileObj)); | |
fileUploaded = request.execute(); | |
Log.d(TAG, "upload success"); | |
} catch (Exception e) { | |
Log.d(TAG, "upload error " + e.toString()); | |
} | |
} | |
}).start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment