file:///android_asset does not work

13,513

Solution 1

I think the URI usage is something like this for assests folder

String imageUri = "assets://image.png";
imageLoader.displayImage(imageUri, imageView);

Just check this reference

So your change your code something like this

ImageLoader.getInstance().displayImage(String.format("assets:///img/categories/%d.JPG", category.getId()), mImageIv);

or even load it from SDCard like this

String imageUri = "file:///mnt/sdcard/image.png"; 

Let me know if this works

Solution 2

file:///android_asset is only for use with WebView.

I do not know what ImageLoader is, but see if it accepts an InputStream. If so, use AssetManager and open() to get an InputStream on your desired asset.

Solution 3

Here's a (simplified!) helper routine that will open the asset as an InputStream if the URI uses the file:///android_asset/ pattern:

public static InputStream open(String urlString, Context context) throws IOException {
    URI uri = URI.create(urlString);

    if (uri.getScheme().equals("file") && uri.getPath().startsWith("/android_asset/")) {
        String path = uri.getPath().replace("/android_asset/", ""); // TODO: should be at start only
        return context.getAssets().open(path);
    } else {
        return uri.toURL().openStream();
    }
}

Usage like:

InputSteam is = Helper.open("file:///android_asset/img/categories/001.JPG", this); // "this" is an Activity, for example

Not shown: exception handling.

Share:
13,513
artem
Author by

artem

Updated on June 23, 2022

Comments

  • artem
    artem about 2 years

    I store the images in assets/img/categories folder and trying to load them with this code:

    ImageLoader.getInstance().displayImage(String.format("file:///android_asset/img/categories/%d.JPG", category.getId()), mImageIv);
    

    It seems to be OK, but does not work:

    E/ImageLoader(28790): /android_asset/img/categories/9.JPG: open failed: ENOENT (No such file or directory)
    E/ImageLoader(28790): java.io.FileNotFoundException: /android_asset/img/categories/9.JPG: open failed: ENOENT (No such file or directory)
    

    Why it does not work?

  • artem
    artem about 10 years
    Thanks. It's quite famous library Universal Image Loader. So, if I want to use this library this way, I need to pre-copy assets to the external storage?
  • CommonsWare
    CommonsWare about 10 years
    @RankoR: Universal Image Loader has its own assets:// pseudo-scheme for assets, as is covered in the documentation.