Search code examples
javaandroidinputstreamp12

inputStream gives java.io.FileNotFoundException


I have created new thread in my android app to read .p12 file like this:

AssetManager am = getAssets();
InputStream inputStream = am.open("C:\\Users\\UkrBikeApp-ff55878cb577.p12");

This file exist in directory. But every time I get

java.io.FileNotFoundException

The simular code works in simple java app without any problems. What is the reason?


Solution

  • First, C:\\Users\\UkrBikeApp-ff55878cb577.p12 is a path to a file on a Windows machine. Android is not Windows.

    Second, C:\\Users\\UkrBikeApp-ff55878cb577.p12 is a filesystem path. It is not a path to an asset within your APK, and so AssetManager would not know what to do with it anyway.

    If you want to package this file in your app:

    • Create an assets/ directory in the appropriate module and source set (e.g., app/src/main/assets/)

    • Put the file in assets/

    • Use open() on AssetManager with the plain filename