在 Android 应用程序中包含一个文件

Include a file in an Android application

我使用 android studio 创建了一个 java 应用程序。在该项目中,我创建了“assets”文件夹,并在其中放置了 json 文件,我将需要该文件来获取 GmailAPI 的凭据。

AssetFolder

我希望我可以访问 json 文件。我已经尝试过很多方法,但每次它都告诉我文件丢失了。 这是代码:

public class GmailApi {

public Gmail initService(String userId) throws GeneralSecurityException,
        IOException {

    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("gmailapi-android.json"))
            .createScoped(GmailScopes.GMAIL_SEND);
    final GoogleCredentials delegatedCreds = creds.createDelegated("xxx@xxx.com");
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCreds);
    HttpTransport transport = new NetHttpTransport.Builder().build();
    // Construct the gmail object.
    Gmail service = new Gmail.Builder(transport, jsonFactory, requestInitializer)
            .setApplicationName("gmailapi")
            .build();

    return service;
}
}

有人能帮帮我吗?我应该在路径中放置什么才能访问 json 文件?

        final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("gmailapi-android.json"))

你试过用这种方式打开文件吗?

AssetManager assetManager = getAssets();
InputStream ims = assetManager.open("gmailapi-android.json");