此时我正在尝试向 Keystore 添加一些证书,因为我需要 Okthpp 接受这些证书。
所以,在开发者 android 中我看到了这个例子:https://developer.android.com/training/articles/security-ssl.html?hl=es#HttpsExample
在他们这样做的例子中:
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.washington.edu/itconnect/security/ca/load-der.crt
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;
我所做的是尝试从我添加到项目结构中的.../res/raw/xxxxx*cer
文件加载证书。
所以我的代码看起来像:
CertificateFactory cf = CertificateFactory.getInstance("X.509");
caInput1 = new BufferedInputStream(new FileInputStream(String.valueOf(ctx.getResources().openRawResourceFd(R.raw.xxxx))));
但是当我尝试时,它不起作用:
我该如何解决?或者不可能?
最佳答案
openRawResourceFd(R.raw.xxxx) 对这种文件失败,它被提到:
This function only works for resources that are stored in the package * as uncompressed data, which typically includes things like mp3 files * and png images.
但是你可以用它来代替
InputStream is = ctx.getResources().openRawResource(R.raw.xxxx);
这不需要使用 FileInputStream,但您可以直接获取它的 InputStream。
关于android - 如何使用 FileInputStream --Android 打开 *.cer 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41166888/