我正在尝试以编程方式将现有的.vcf
文件上传到Google云端硬盘。到目前为止,它只保存一个空文件。
这可能很简单,但我不熟悉Google Drive Api。 这是代码:
// Perform I/O off the UI thread.
new Thread() {
@Override
public void run() {
// write content to DriveContents
File f = new File("גיבוי אנשי קשר" + ".vcf");
filepath = f.getAbsolutePath().toString();
//String inFileName = Environment.getExternalStorageDirectory().getPath() + "גיבוי אנשי קשר" + ".vcf" ;
String inFileName = filepath;
File backupFile = new File(inFileName);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(backupFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte[8 * 1024];
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(driveContents.getOutputStream());
int n = 0;
try {
while ((n = bufferedInputStream.read(buffer)) > 0) {
bufferedOutputStream.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//final MimeTypeMap mime = MimeTypeMap.getSingleton();
// String tmptype = mime.getMimeTypeFromExtension("vcf");
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("גיבוי אנשי קשר")
.setMimeType("text/x-vcard")
.setStarred(true).build();
答案 0 :(得分:1)
检查Uploading Files您将使用Files.create上传文件的位置。
请务必指定char str[] = "jkimsdfs";
char *ptr;
if ((ptr = strstr(str, "kim"))) {
if ((ptr - str) == 1) {
/* Match */
}
}
,因为它是必需参数。
以下是文档的摘录:
uploadType
答案 1 :(得分:0)
这里是我用来将“csv”文件保存为电子表格的一些代码。 它需要Drive API(用于文件夹)和Sheets API(用于文件)。
使用此快速入门: https://developers.google.com/drive/v3/web/quickstart/android
如有任何问题,请询问。 希望它有所帮助!
@Override
protected Boolean doInBackground(Void... params) {
try {
deviceOnline = isDeviceOnline();
if (!deviceOnline) {
Log.e(LOG_CONNECT, "in doInBackground() - Device is not connected to network");
cancel(true);
return false;
}
Boolean uploadToDriveSuccessfull = uploadFileToDrive();
if (uploadToDriveSuccessfull) {
Log.d(LOG_CONNECT, "in doInBackground() - File Uploaded To Drive = True ");
return true;
}
else {
Log.e(LOG_CONNECT, "in doInBackground() - File Uploaded To Drive = False ");
//TODO - Upload failed - trigger second try to save to Drive
return false;
}
} catch (Exception e) {
Log.e(LOG_CONNECT, "in doInBackground() - error: " + e.getMessage());
mLastError = e;
cancel(true);
return false;
}
}
private Boolean uploadFileToDrive() {
Log.d(LOG_CONNECT, "in uploadFileToDrive()");
//Create the DriveFolderID once
if (getDriveFolderID() == null ) {
Log.d(LOG_CONNECT, "in uploadFileToDrive() - creating new folder");
setDriveFolderID(createFolderInDrive());
}
String fileId;
fileId = saveOneFileToDrive(mBackupFileUri,mDriveFolderId, mBackupFileName);
if(fileId != null){
//updates the shared pref
setmDriveSavedFileId(fileId);
setFileName(mBackupFileName,driveTag);
Log.d(LOG_CONNECT, "in uploadFileToDrive() - file saved, ID: " + fileId);
return true;
}
else{
Log.e(LOG_CONNECT, "in uploadFileToDrive() - file NOT saved");
return false;
}
}
private String createFolderInDrive () {
Log.d(LOG_CONNECT, "in createFolderInDrive()");
File fileMetadata = new File();
fileMetadata.setName(getString(R.string.app_name));
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = null;
try {
file = mDriveService.files().create(fileMetadata)
.setFields("id")
.execute();
} catch (IOException e) {
Log.e(LOG_CONNECT, "in createFolderInDrive() - IOException: " + e.getMessage());
e.printStackTrace();
return null;
}
Log.d(LOG_CONNECT, "in createFolderInDrive() - FOLDER ID: " + file.getId());
Log.d(LOG_CONNECT, "in createFolderInDrive() - FOLDER stared = " + file.getStarred());
return file.getId();
}
//This function Save new file to Drive
private String saveOneFileToDrive (Uri localFileUri,String driveFolderId, String fileNameOnDrive){
Log.d(LOG_CONNECT, "in saveOneFileToDrive()");
if (localFileUri == null) {
Log.e(LOG_CONNECT, "in saveOneFileToDrive() - fileUri is null");
return null;
}
String filePathString = localFileUri.getPath();
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - fileString= " + filePathString);
File fileMetadata = new File();
//Set csv file name with real one with date.
fileMetadata.setName(fileNameOnDrive);
String fields;
if (driveFolderId != null) {
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - FOLDER ID is: " + driveFolderId);
fileMetadata.setParents(Collections.singletonList(driveFolderId));
fields = "id, parents";
} else {
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - folder is null");
fields = "id";
}
//Saving SpreadSheet - you can change it
fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");
java.io.File filePath = new java.io.File(filePathString);
FileContent mediaContent = new FileContent("text/csv", filePath);
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - mediaContent size= " + mediaContent.getLength());
//print the file to log//
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(mediaContent.getFile()));
} catch (FileNotFoundException e) {
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - FileNotFoundException= " + e.getMessage());
e.printStackTrace();
}
String line = null;
try {
while ((line = br.readLine()) != null) {
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - File info= " + line);
}
} catch (IOException e) {
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - IOException= " + e.getMessage());
e.printStackTrace();
}
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - FINISHED PRINT");
//End of print to log//
File file = null;
//updating the member value from shared pref.
getmDriveSavedFileId();
try {
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - starting to create file");
file = mDriveService.files().create(fileMetadata, mediaContent)
.setFields(fields)
.execute();
} catch (UserRecoverableAuthIOException e) {
Log.e(LOG_CONNECT, "in saveOneFileToDrive() - UserRecoverableAuthIOException error: " + e.getCause());
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
return null;
} catch (IOException e) {
Log.e(LOG_CONNECT, "in saveOneFileToDrive() - IOException error: " + e.getCause()
+ ", message: " + e.getMessage());
e.printStackTrace();
return null;
}
Log.d(LOG_CONNECT, "in saveOneFileToDrive() - File ID: " + file.getId());
return file.getId();
}