0

I am trying to run this code in my Android App:

    public Bigquery createAuthorizedClient() throws IOException {
    final List SCOPE = Arrays.asList("https://www.googleapis.com/auth/bigquery");
    HttpTransport transport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    GoogleCredential credential = new GoogleCredential();
    try{
        File file = new File("C:\\testprojectbigquery\\UkrBikeApp-ff55878cb577.p12");
        credential = new GoogleCredential.Builder().setTransport(transport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId("bigquerymain@ukrbikeapp.iam.gserviceaccount.com")
                .setServiceAccountScopes(SCOPE)
                .setServiceAccountPrivateKeyFromP12File(file)
                .build();
    }
    catch (Exception e){
        System.out.println("e = " + e);
    }


    if (credential.createScopedRequired()) {
        credential = credential.createScoped(BigqueryScopes.all());
    }

    return new Bigquery.Builder(transport, jsonFactory, credential)
            .setApplicationName("Bigquery Samples")
            .build();
}

But every time I get:

java.io.FileNotFoundException

Then I tried to create InputStream but then didn't change situation:

        AssetManager am = getAssets();
        InputStream inputStream = am.open("C:\\testprojectbigquery\\UkrBikeApp-ff55878cb577.p12");
        File file = createFileFromInputStream(inputStream);
DzouSi
  • 361
  • 3
  • 7
  • 21

1 Answers1

1

You are coding for an android device, and android does not have C drive! To read this file, you must put your file into assets folder of project, then read from this folder. Here is an example of reading assets

TuyenNTA
  • 1,194
  • 1
  • 11
  • 19
  • Thanks for your answer. I still can't find how can I read file object from assets to use it in my GoogleCredential. Is there any way? – DzouSi May 22 '17 at 23:05
  • For your purpose to get a `File`, you can using the URL like this: `file:///android_asset/...` where `...` is the relative path to your asset in asset folder. So you can try: `File file = new File("file:///android_asset/UkrBikeApp-ff55878cb577.p12");` with the file `UkrBikeApp-ff55878cb577.p12` putted inside asset folder. For you [refer](https://stackoverflow.com/questions/4820816/how-to-get-uri-from-an-asset-file) – TuyenNTA May 23 '17 at 22:11