简体   繁体   English

Android Studio无法在资产文件夹中找到文件

[英]Android Studio Can't find file in Assets Folder

New to Android Studio and am stumbling on fairly simple things. Android Studio的新功能,我在相当简单的事情上磕磕绊绊。 I'm trying to open a file that I have stored in the assets folder of my project. 我正在尝试打开一个存储在项目的assets文件夹中的文件。 I get a FileNotFoundException error. 我收到FileNotFoundException错误。 How should I correctly store and reference the file? 我该如何正确存储和引用该文件?

Here is the method that crashes: 这是崩溃的方法:

    public void saveKey(String outFile, String pubKeyFilename) throws IOException, GeneralSecurityException {

        File out = new File(outFile);
        File publicKeyFile = new File(pubKeyFilename);

        // read public key to be used to encrypt the AES key
        byte[] encodedKey = new byte[(int)publicKeyFile.length()];

        // crashes here
        new FileInputStream(publicKeyFile).read(encodedKey);

        //...
}

And here is how I am calling it: 以下是我如何称呼它:

EncryptRSA cipher = new EncryptRSA();
cipher.saveKey("temp", "public.der");

The file is saved in my assets folder like this: 该文件保存在我的资产文件夹中,如下所示: 在此输入图像描述

I'm trying to open a file that I have stored in the assets folder of my project. 我正在尝试打开一个存储在项目的assets文件夹中的文件。

To access file from assets folder use AssetManager for getting InputStream using file name: 要从assets文件夹访问文件,请使用AssetManager使用文件名获取InputStream:

AssetManager assManager = getApplicationContext().getAssets();
InputStream inputStream = assManager.open(pubKeyFilename);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = reader.readLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM