. \ key.p12: open failed: ENOENT (There is no such file or directory)

In my Android application, I am accessing Google Cloud Storage. I created the private key xxxxxxxkey.p12 . I put my key file in the resource folder. But when you start the project, it does not open the key.p12 file. I tried to put it outside the resource folder, but there is 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 did the same in a java project. What matters because of the android? or wrong file path?

+4
source share
1 answer

After some battle, I got my answer, Thank you very much for your support. Thumbs up!

AssetManager, raw resource

AssetManager

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

, raw res

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

, , android, (/) 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;
    }

!

0

All Articles