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
相关产品推荐
相关产品推荐

