2017-05-30 19 views
0

既存の.vcfファイルをプログラムでGoogleドライブにアップロードしようとしています。これまでは空のファイルしか保存していませんでした。GoogleドライブAPIを使用してファイルをアップロードする

これは簡単かもしれませんが、私はGoogleドライブ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(); 

答えて

1

チェックファイルをアップロードするFiles.createを使用することがありますUploading Files: は、ここでは、コードです。

必ずuploadTypeを必須パラメータとして指定してください。ここで

は、ドキュメントからの抜粋です:

File fileMetadata = new File(); 
fileMetadata.setName("photo.jpg"); 
java.io.File filePath = new java.io.File("files/photo.jpg"); 
FileContent mediaContent = new FileContent("image/jpeg", filePath); 
File file = driveService.files().create(fileMetadata, mediaContent) 
     .setFields("id") 
     .execute(); 
System.out.println("File ID: " + file.getId()); 
+0

この例では、私はこれが思い付いたものですgithubのは、GoogleのAPIの例を踏襲関係ありません。あなたは私の与えられたコードで任意のソリューションを考えることができますか? –

関連する問題