图像文件未找到异常android

时间:2013-11-18 08:55:37

标签: android file assets

我尝试根据服务提供的路径动态地向网格添加图标, 我已经在assets文件夹中添加了图标,我发现找不到文件异常,我该如何修复它。

以下是我的代码,

            try
                 {
                InputStream im = context.getAssets().open(App.Path);
                Drawable d = Drawable.createFromStream(im, null);

            }
            catch(IOException e)
            {
                 e.printStackTrace();
            }

enter image description here

4 个答案:

答案 0 :(得分:1)

我看到你有“Profile Registration.png” - >重命名文件,使其不包含空格。类似于:“profile_registration.png”

答案 1 :(得分:0)

请检查您的文件是否可用。

如果有文件,请按照以下说明进行操作

而不是

InputStream im = context.getAssets().open(App.Path);

请使用此

InputStreamReader im = new InputStreamReader(getApplicationContext().getAssets().open("path name upto file")));

请使用此功能。它将起作用

答案 2 :(得分:0)

实际上问题在于前缀/ assets /:)

 try {
        String image = App.Path;

        if (image.contains("/assets/")) {
            image = image.replace("/assets/", "");
        }

        InputStream ims = getAssets().open(image);
        Drawable d = Drawable.createFromStream(ims, null);
        imageView.setImageDrawable(d);

    } catch (IOException ex) {
        return;
    }

答案 3 :(得分:0)

试试这个:

 try {
        String image_path = App.Path;
        String filepath = null;
        if (image_path.contains("/ASSETS/")) {
             filepath = image_path .replace(ASSETS, "");
        }    
        InputStreamReader im = new InputStreamReader(getApplicationContext().getAssets().open(filepath)));
        Drawable d = Drawable.createFromStream(ims, null);
        imageView.setImageDrawable(d);

    } catch (IOException ex) {
        return;
    }