Home > Enterprise >  Include a file in an Android application
Include a file in an Android application

Time:05-17

I used android studio to create a java app. In the project, I created the "assets" folder, and inside I put the json file that I will need to get the credentials for GmailAPI.

AssetFolder

I wish I could access the json file. I have already tried in many ways but every time it tells me that the file is missing. This is the code:

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("[email protected]");
    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;
}
}

Could anyone help me? What should I put in the path to be able to access the json file?

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

CodePudding user response:

Did you try to open the file this way?

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