.\key.p12: open failed: ENOENT (No such file or di

2019-04-10 11:44发布

In my android application I'm accessing the Google cloud storage . I have generated the private key xxxxxxxkey.p12 .I have put my key file in assets folder . But while running the project it is not opening the key.p12 file . I have tried putting it outside the assets folder , still no result.

     httpTransport = AndroidHttp.newCompatibleTransport(); 
            AssetManager am = getAssets();
            InputStream inputStream = am.open("xxxxxxxxxxKey.p12");
            File file = createFileFromInputStream(inputStream);

       GoogleCredential credential = new GoogleCredential.Builder()
                            .setTransport(httpTransport)
                            .setJsonFactory(JSON_FACTORY)
                            .setServiceAccountId(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com")
                            .setServiceAccountScopes(Collections.singleton(STORAGE_SCOPE))
                            .setServiceAccountPrivateKeyFromP12File(file).build();

createFileFromInputStream()

private File createFileFromInputStream(InputStream inputStream) {

        try {
            File f = new File("download/MyKey.p12");
            OutputStream outputStream = new FileOutputStream(f);
            byte buffer[] = new byte[1024];
            int length = 0;

            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            outputStream.close();
            inputStream.close();

            return f;
        } catch (IOException e) {
            // Logging exception
        }

        return null;
    }

I've done the same in java project.What makes the difference, is it because of android ? or the path to the file location is incorrect?

1条回答
Juvenile、少年°
2楼-- · 2019-04-10 12:46

After some struggle I have got my answer, Thanks a lot for your support. Thumbs up!

File can be retrieved using using AssetManager and also we can get it as a raw resource

Using AssetManager

        AssetManager am = getAssets();
        InputStream inputStream = am.open("xxxxxxxxxxKey.p12");
        File file = createFileFromInputStream(inputStream);

As a raw resource , put the file in raw folder inside res directory

        InputStream ins = getResources().openRawResource(R.raw.keyfile);
        File file = createFileFromInputStream(ins);

While writing the output file you have to specify where your keyfile actually belongs , in my case I'm using android, I'm creating the file inside the internal storage(emulator/device) inside folder KeyHolder/KeyFile

private File createFileFromInputStream(InputStream inputStream) {

        String path = "";

        File file = new File(Environment.getExternalStorageDirectory(),
                "KeyHolder/KeyFile/");
        if (!file.exists()) {
            if (!file.mkdirs())
                Log.d("KeyHolder", "Folder not created");
            else
                Log.d("KeyHolder", "Folder created");
        } else
            Log.d("KeyHolder", "Folder present");

       path = file.getAbsolutePath();

        try {
            File f = new File(path+"/MyKey");
            OutputStream outputStream = new FileOutputStream(f);
            byte buffer[] = new byte[1024];
            int length = 0;

            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }

            outputStream.close();
            inputStream.close();

            return f;
        } catch (IOException e) {
            // Logging exception
            e.printStackTrace();
        }

        return null;
    }

and that's it !

查看更多
登录 后发表回答