file:///android_asset does not work

爷,独闯天下 提交于 2019-12-12 16:18:01

问题


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?


回答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




回答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.




回答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.



来源:https://stackoverflow.com/questions/23954878/file-android-asset-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!