简体   繁体   English

class.getResourceAsStream 在 android studio 中返回 null

[英]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 视图中不可见。

Android View安卓视图

Project View项目视图

Class#getResource and Class#getResourceAsStream looks for the resource relative to the class/in the same package. Class#getResourceClass#getResourceAsStream在同一个包中查找相对于类/的资源。 This leaves you with multiple options:这为您提供了多种选择:

move 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解决问题。

Use 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文件夹)的资源。

Append a leading / 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM