google 个驱动器文件的文件路径为空

File path is empty for google drive files

我正在开发文件上传 android 应用程序。我的 objective 是将用户选择的文件从文件管理器上传到远程服务器。 But when a google drive file is selected , file uploading fails because of empty path .有人可以帮助我吗?

我的代码是:

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == PICK_FILE_REQUEST) {
            if (data == null) {
                //no data present
                return;
            }
            Uri selectedFileUri = data.getData();
            selectedFilePath = FilePath.getPath(mActivity, selectedFileUri);
            if (selectedFilePath != null && !selectedFilePath.equals("")) {
                callUploadDocumentAPI();
            } else {
                Toast.makeText(mActivity, StringConstants.CANT_UPLOAD_TO_SERVER, Toast.LENGTH_SHORT).show();
            }
        }
   }

But when a google drive file is selected , file uploading fails because of empty path .

FilePath.getPath(mActivity, selectedFileUri) 无法工作。 Uri 不是文件。

使用 ContentResolveropenInputStream() 得到 InputStreamUri 识别的内容。要么直接使用 InputStream,要么使用它来制作您自己控制的某个文件中内容的副本,然后使用该副本。