0

I'm attempting to access a zip file I have stored in my assets folder using the following:

val filePath = Uri.parse("file:///android_asset/testfile_v1.1.1.zip").toString()

However I'm getting a FileNotFound error even though I know for sure the file is there and I have no idea why.

Any suggestions are appreciated.

Error:

java.io.FileNotFoundException: file:/android_asset/testfile_v1.1.1.zip (No such file or directory)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:200)
        at java.io.FileInputStream.<init>(FileInputStream.java:150)
        at java.io.FileInputStream.<init>(FileInputStream.java:103)

Use Case:

private InputStream openInputStream(final String filePath, final String mimeType, final int mbrSize, final int types) throws IOException {
    final InputStream is = new FileInputStream(filePath);
    if (MIME_TYPE_ZIP.equals(mimeType))
        return new ArchiveInputStream(is, mbrSize, types);
    if (filePath.toLowerCase(Locale.US).endsWith("hex"))
        return new HexInputStream(is, mbrSize);
    return is;
}
user13125638
  • 77
  • 1
  • 2
  • 5
  • possible duplicate of https://stackoverflow.com/a/45908819/11377112 – Animesh Sahu Apr 14 '20 at 06:51
  • 3
    Does this answer your question? [How to access a file from asset/raw directory](https://stackoverflow.com/questions/45908648/how-to-access-a-file-from-asset-raw-directory) – Animesh Sahu Apr 14 '20 at 06:53

1 Answers1

0

In case the path /android_asset/testfile_v1.1.1.zip is really correct, it might be because you missed one slash in the unescaped path you input, i.e. file:/// instead of file:////

Tom Yan
  • 201
  • 1
  • 6
  • I'm getting the exact same error when adding the 4th slash - however - I'm not sure if it makes a difference - but I'm passing this value from my app to this external library - which is where the file is consumed: https://github.com/NordicSemiconductor/Android-DFU-Library – user13125638 Apr 14 '20 at 06:08
  • Also - I am 100% sure the path is correct – user13125638 Apr 14 '20 at 06:25
  • Sorry I was wrong anyway. I forgot the slash for `/`. – Tom Yan Apr 14 '20 at 06:37
  • @user13125638 I am not sure you should use any form of URL though. `FileInputStream` takes a File object or system-dependent path. Maybe you should just use `"/android_asset/testfile_v1.1.1.zip"`. – Tom Yan Apr 14 '20 at 06:41
  • That isn't working either - I have a feeling this is because I'm using a 3rd party library which is attempting to consume a file in my assets folder which it does not have permission to access. Any idea what to do in that case? – user13125638 Apr 14 '20 at 06:46
  • Are you using the path with `openInputStream` (instead of `Uri.parse`) tho? – Tom Yan Apr 14 '20 at 07:02
  • For permission it's a matter of whether you run it as root I think. – Tom Yan Apr 14 '20 at 07:05
  • `file:///android_asset/testfile_v1.1.1.zip` is only understood by a WebView. Animesh gave you the right direction already. – blackapps Apr 14 '20 at 07:23