\ key.p12:打开失败:ENOENT(没有这样的文件或目录) [英] .\key.p12: open failed: ENOENT (No such file or directory)
问题描述
在我的Android应用程序,我访问了谷歌云存储。我已经生成私钥 xxxxxxxkey.p12 。我已经把我的资产文件夹中的密钥文件。但是,在运行项目时,它是不开放的key.p12文件。我试图把它的资产文件夹之外,还是没有结果。
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;
}
我已经做了在Java project.What同样产生差别,是由于Android呢?或文件路径位置不正确?
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?
推荐答案
经过一番挣扎后,我有我的回答,非常感谢您的支持。竖起大拇指!
After some struggle I have got my answer, Thanks a lot for your support. Thumbs up!
文件可通过使用检索 AssetManager ,然后我们也可以把它作为一个原始资源
File can be retrieved using using AssetManager and also we can get it as a raw resource
使用AssetManager
AssetManager am = getAssets();
InputStream inputStream = am.open("xxxxxxxxxxKey.p12");
File file = createFileFromInputStream(inputStream);
作为原始资源,把文件中的原始文件夹中res目录
As a raw resource , put the file in raw folder inside res directory
InputStream ins = getResources().openRawResource(R.raw.keyfile);
File file = createFileFromInputStream(ins);
在写输出文件,你必须指定你的密钥文件实际上属于,在我的情况,我采用了android,我创建文件夹内的内部存储里面的文件(模拟器/设备)的匙扣/ 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 !
这篇关于\ key.p12:打开失败:ENOENT(没有这样的文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!