在我的 android 应用程序中,我正在访问 Google 云存储。我已经生成了私钥 xxxxxxxkey.p12 。我已将我的 key 文件放在 assets 文件夹中。但是在运行项目时它没有打开 key.p12 文件。我试过把它放在 assets 文件夹之外,仍然没有结果。

     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;
    }

我在java项目中做了同样的事情。有什么不同,是因为android吗?或者文件位置的路径不正确?

最佳答案

经过一番挣扎,我得到了答案,非常感谢您的支持。竖起大拇指!

可以使用 AssetManager 检索文件,我们也可以将其作为 原始资源 获取

使用 AssetManager

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

作为 原始资源 ,将文件放在 res 目录中的 raw 文件夹中
        InputStream ins = getResources().openRawResource(R.raw.keyfile);
        File file = createFileFromInputStream(ins);

在编写输出文件时,您必须指定 key 文件实际所属的位置,就我而言,我使用的是 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;
    }

就是这样!

关于java - .\key.p12 : open failed: ENOENT (No such file or directory),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30888783/

10-10 09:25