我正在创建一个应用程序来连接到Google Calendar API,但每当我尝试加载客户端机密时,IOException都会跳转。
我试过改变存储文件的路径(首先是在app文件夹中,然后我将其移动到res/assets
)以改变使用InputStream
打开文件的方式:1º首先代码是这样的:
private static final String CREDENTIALS_FILE_PATH = "assets/credentials.json";
InputStream in = this.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
然后我尝试从资产中打开它:
InputStream in = this.getAssets().open("credentials.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
但无论我尝试什么,它都给我java.io.FileNotFoundException: credentials.json
错误,我不确定是不是因为路径名称,打开它的方式或其他什么。
经过调查,我发现了如何访问json。首先是将文件从res/assets
移动到res/raw
然后改变这一行:
InputStream in = this.getAssets().open("credentials.json");
对这一个
InputStream in = this.getResources().openRawResource(R.raw.credentials);
现在文件将被读取没有问题