[英]class.getResourceAsStream gives back null in android studio
I'm trying to create an app in android studio that can access a google sheet.我正在尝试在 android studio 中创建一个可以访问 google sheet 的应用程序。 My credentials.json file is like this
我的 credentials.json 文件是这样的
{"client_id":[My ID], "project_id":"androidandsheets", "auth_uri":[My auth URI], "token_uri":[My token URI], "auth_provider_x509_cert_url":[URL]}
but when I use this java code但是当我使用这个java代码时
private Credential authorize() throws IOException, GeneralSecurityException{
InputStream in = SheetsAndJava.class.getResourceAsStream("credentials.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
GsonFactory.getDefaultInstance(),new InputStreamReader(in)
);
List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
GsonFactory.getDefaultInstance(),
clientSecrets,scopes
).setDataStoreFactory(new FileDataStoreFactory(new File("tokens")))
.setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(
flow,new LocalServerReceiver()
).authorize("user");
return credential;
}
the input stream returned by InputStream in = SheetsAndJava.class.getResourceAsStream("credentials.json") is null. InputStream in = SheetsAndJava.class.getResourceAsStream("credentials.json") 返回的输入流为空。 Can someone please me fix this issue.
有人可以请我解决这个问题。 Here's my layout for this project.
这是我对这个项目的布局。 For some reason the credentials.json file is visible in project view but not in android view.
由于某种原因,credentials.json 文件在项目视图中可见,但在 android 视图中不可见。
Class#getResource
and Class#getResourceAsStream
looks for the resource relative to the class/in the same package. Class#getResource
和Class#getResourceAsStream
在同一个包中查找相对于类/的资源。 This leaves you with multiple options:这为您提供了多种选择:
credentials.json
credentials.json
Since it is looking for the credentials.json
file in com/example/excelintro
, the problem should be solved by just moving it to res/com/example/excelintro
.由于它正在
com/example/excelintro
中查找credentials.json
文件,因此只需将其移至res/com/example/excelintro
解决问题。
ClassLoader#getResource
/ ClassLoader#getResourceAsStream
ClassLoader#getResource
/ ClassLoader#getResourceAsStream
Instead of callng SheetsAndJava.class.getResourceAsStream(...)
(or SheetsAndJava.class.getResource(...)
) (which resolves the resource relative to the class), you can use SheetsAndJava.class.getClassLoader().getResourceAsStream(...)
(or SheetsAndJava.class.getClassLoader().getResource(...)
).您可以使用
SheetsAndJava.class.getResource(...)
( SheetsAndJava.class.getResourceAsStream(...)
SheetsAndJava.class.getClassLoader().getResourceAsStream(...)
(或SheetsAndJava.class.getClassLoader().getResource(...)
)。 This resolves the resource relative to the classpath root (the res
folder).这会解析相对于类路径根目录(
res
文件夹)的资源。
/
to the resource name/
附加到资源名称If you use /credentials.json
instead of just credentials.json
, it should also resolve the resource from the classpath root.如果您使用
/credentials.json
而不仅仅是credentials.json
,它还应该从类路径根目录解析资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.