You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio项目访问Assets资源失败,调用DriveAPI遇IOException

连接Google Drive API时读取凭证文件触发IOException

我尝试使用Drive API将应用连接至Google Drive,但在获取凭证路径的输入流时触发了IOException。

相关代码

获取凭证的方法:

public Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT)
            throws IOException {
        // Load client secrets.
        InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);//getApplicationContext().getAssets().open(CREDENTIALS_FILE_PATH);//MainActivity.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);

        }
        GoogleClientSecrets clientSecrets =
                GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
        //returns an authorized Credential object.
        return credential;
    }

凭证路径定义:

private static final String CREDENTIALS_FILE_PATH = "CapturaVideo/app/src/main/assets/credentials.json";  // "credentials.json";

项目文件结构

credentials.json 文件位于项目的 app/src/main/assets 目录下

已尝试的读取方式

我试过两种资源读取方式,但均出现相同错误:

//getApplicationContext().getAssets().open(CREDENTIALS_FILE_PATH);
//MainActivity.class.getResourceAsStream(CREDENTIALS_FILE_PATH);

内容的提问来源于stack exchange,提问作者MISAEL DAVID LINARES WARTHON

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 02:06:24