简体   繁体   English

file:/// android_asset不起作用

[英]file:///android_asset does not work

I store the images in assets/img/categories folder and trying to load them with this code: 我将图像存储在assets / img / categories文件夹中,并尝试使用以下代码加载它们:

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? 为什么它不起作用?

file:///android_asset is only for use with WebView . file:///android_asset仅用于WebView

I do not know what ImageLoader is, but see if it accepts an InputStream . 我不知道ImageLoader是什么,但看看它是否接受了InputStream If so, use AssetManager and open() to get an InputStream on your desired asset. 如果是这样,请使用AssetManageropen()在所需资产上获取InputStream

I think the URI usage is something like this for assests folder 我认为对于assests文件夹,URI的用法是这样的

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 甚至像这样从SDCard加载它

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

Let me know if this works 让我知道这个是否奏效

Here's a (simplified!) helper routine that will open the asset as an InputStream if the URI uses the file:///android_asset/ pattern: 这是一个(简化的!)辅助例程,如果URI使用file:///android_asset/ pattern,它将打开资产作为InputStream

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. 未显示:异常处理。

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

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