2016-10-22 6 views
0

AndroidのDrive.DriveApiを使用しています。エクスポートしてAndroid搭載端末からGoogleドライブにファイルをインポートすると、両方向で動作します。AndroidでGoogleドライブファイルにアクセスすることはできません。最初にブラウザからアップロードしたとき

また、ファイルにアクセスしてWebブラウザからローカルのMacにダウンロードすることもできます。

MacでGoogleドライブに新しいファイルをアップロードしてから、後でAndroidアプリケーションからこのファイルにアクセスして読み込むと、ファイルにアクセスできません。私はファイルを見ることができますが、それはグレー表示されています。

私はMacのオンラインでGoogleドライブでAndroidアプリで同じユーザーを使用しています。

誰でも?

私のコードを使用しています。 インポート:

 IntentSender intentSender = Drive.DriveApi 
       .newOpenFileActivityBuilder() 
       .setMimeType(new String[]{"application/csv"}) 
       .build(activity.getGoogleClient()); 
     try { 
      activity.startIntentSenderForResult(intentSender, DRIVE_REQUEST_CODE_OPENER_IMPORT, null, 0, 0, 0); 
     } catch (IntentSender.SendIntentException e) { 
      Log.w(TAG, "Unable to send intent: ", e); 
     } 

輸出:

 Drive.DriveApi.newDriveContents(activity.getGoogleClient()).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 
      @Override 
      public void onResult(DriveApi.DriveContentsResult driveContentsResult) { 
       Uri csvUri = Uri.parse("file://" + getCsvFile(context)); 
       File csvFile = new File(csvUri.getPath()); 
       if(csvFile.length() <= 0) { 
        Log.e(TAG, "File empty: " + getCsvFile(context)); 
       } 
       FileInputStream csvInputStream = null; 
       try { 
        csvInputStream = new FileInputStream(csvFile); 
        OutputStream outputStream = driveContentsResult.getDriveContents().getOutputStream(); 
        byte[] dataArray = IOUtils.toByteArray(csvInputStream); 
        outputStream.write(dataArray); 
       } catch (FileNotFoundException fnfe) { 
        Log.e(TAG, "File not found: ", fnfe); 
       } catch (IOException ie) { 
        Log.e(TAG, "Error uploading file: ", ie); 
       } 
       MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder().setMimeType("application/csv").setTitle(getCsvFilename()).build(); 
       IntentSender intentSender = Drive.DriveApi 
         .newCreateFileActivityBuilder() 
         .setInitialMetadata(metadataChangeSet) 
         .setInitialDriveContents(driveContentsResult.getDriveContents()) 
         .build(activity.getGoogleClient()); 
       try { 
        activity.startIntentSenderForResult(intentSender, DRIVE_REQUEST_CODE_OPENER_EXPORT, null, 0, 0, 0); 
       } catch (IntentSender.SendIntentException e) { 
        Log.w(TAG, "Unable to send intent: ", e); 
       } 
      } 
     }); 

onActivityResult:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    final Uri csvUri = Uri.parse("file://" + getCsvFile(context)); 
    final File csvFile = new File(csvUri.getPath()); 
    if(requestCode == DRIVE_REQUEST_CODE_OPENER_IMPORT && resultCode == activity.RESULT_OK && data != null) { 
     DriveId driveId = (DriveId) data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID); 
     Log.i(TAG, "Selected file's ID: " + driveId); 
     driveId.asDriveFile().open(activity.getGoogleClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 
      @Override 
      public void onResult(DriveApi.DriveContentsResult driveContentsResult) { 
       try { 
        InputStream input = driveContentsResult.getDriveContents().getInputStream(); 
        byte[] dataArray = IOUtils.toByteArray(input); 
        FileUtils.writeByteArrayToFile(csvFile, dataArray); 
        finish.onImportFinished(); 
       } catch (IOException ie) { 
        Log.e(TAG, "Error downloading file: ", ie); 
       } 
      } 
     }); 
    } else if(requestCode == DRIVE_REQUEST_CODE_OPENER_EXPORT && resultCode != activity.RESULT_CANCELED) { 
     finish.onExportFinished(); 
    } 
    csvFile.delete(); // Always delete to avoid readable file on disk 
} 

ファイルvault.csv選択できない

enter image description here

アップデート1:

public void initGoogleClient() { 
    if (mGoogleApiClient == null) { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(Drive.API) 
       .addScope(Drive.SCOPE_FILE) 
       .addScope(Drive.SCOPE_APPFOLDER) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 
    } 
    mGoogleApiClient.connect(); 
} 

アップデート2:

私はしかし、最初に私のAndroidアプリの中からファイルをエクスポートする場合。その後、Macのブラウザからファイルをダウンロードし、データを追加します。私のMacからアップロードしてください。 Androidアプリからこのファイルをインポートすると機能します。

WEB Googleドライブインターフェイスから新しくアップロードしたファイルの権限を変更する方法はありますか?他のコメント?

Update3と私のMac版Googleドライブブラウザ:

enter image description here

+1

にMIMEタイプを変更しましたか? – muratgu

+0

良い点、私がスコープを定義する場所を見つけようとしています。何か案が?誓いの時に – powder366

+0

。 – muratgu

答えて

0

は、両方のアプリに認証要求時に使うのですかどのような範囲(複数可)

.setMimeType(new String[]{"text/csv"}) 
関連する問題