我需要打开,阅读并获取.pfx文件的路径。该文件位于我的android studio项目文件夹MyApp / certificate / mycertificate.pfx中。
public void openFile(){
CkCert cert = new CkCert();
File file = new File("/certificate/mycertificate.pfx");
String pfxFilename = file.getAbsolutePath();
String pfxPassword = "1234";
cert.LoadPfxFile(pfxFilename,pfxPassword)};
我收到以下错误:没有这样的文件或目录。有什么建议吗?
已解决: 我将文件移到\ app \ src \ main \ assets
public void open(Context context){
InputStream cert= context.getAssets().open("mycert.pfx");
char[] password = null;
KeyStore keystore = null;
Enumeration<String> aliases;
String alias = "";
//open the file
senha = "1234".toCharArray();
keystore = KeyStore.getInstance("PKCS12");
keystore.load(cert, password);
//Get the alias
KeyStore.PrivateKeyEntry pkEntry = null;
PrivateKey pk = null;
try {
aliases = keystore.aliases();
while(aliases.hasMoreElements()) {
alias = aliases.nextElement();
System.out.println(alias);
if (keystore.isKeyEntry(alias)) {
pkEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(password));
pk = pkEntry.getPrivateKey();
}
}
} catch (KeyStoreException e) {
throw new RuntimeException("CATCH", e);
}
}
答案 0 :(得分:0)
它仅在我的android studio项目的根文件夹中
该文件在您的开发计算机上,而不在任何设备上。而是将其打包为资产(模块的src/main/assets/
),然后使用AssetManager
来open()
在该资产上InputStream
。