如何从资产加载所有图像?

时间:2015-08-19 19:09:12

标签: android

我尝试从资源文件夹加载图片,但我收到FileNotFoundException

我使用了2种方法:

try {
        AssetManager am = MainActivity.this.getAssets();
        String[] files = am.list("cards");
        InputStream istr = null;
        for (String file : files) {
            Log.d("MyLogs", file);
            istr = am.open(file);
            Drawable d = Drawable.createFromStream(istr, null);
            drawables.add(d);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

并使用Bitmap ....同样的问题.... logcat的:

08-19 18:51:17.043    5246-5246/com.example.admin.background D/MyLogs﹕ ic_credit_card_black_48dp.png
08-19 18:51:17.043    5246-5246/com.example.admin.background W/System.err﹕ java.io.FileNotFoundException: ic_credit_card_black_48dp.png
08-19 18:51:17.111    5246-5246/com.example.admin.background W/System.err﹕ at android.content.res.AssetManager.openAsset(Native Method)
08-19 18:51:17.111    5246-5246/com.example.admin.background W/System.err﹕ at android.content.res.AssetManager.open(AssetManager.java:315)
08-19 18:51:17.115    5246-5246/com.example.admin.background W/System.err﹕ at android.content.res.AssetManager.open(AssetManager.java:289)
08-19 18:51:17.115    5246-5246/com.example.admin.background W/System.err﹕ at com.example.admin.background.MainActivity.onCreate(MainActivity.java:58)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5104)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 18:51:17.119    5246-5246/com.example.admin.background W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
08-19 18:51:17.123    5246-5246/com.example.admin.background W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5041)
08-19 18:51:17.123    5246-5246/com.example.admin.background W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-19 18:51:17.123    5246-5246/com.example.admin.background W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
08-19 18:51:17.123    5246-5246/com.example.admin.background W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-19 18:51:17.123    5246-5246/com.example.admin.background W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-19 18:51:17.123    5246-5246/com.example.admin.background W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您忘了包含文件夹。这应该有效:

istr = am.open( "cards/" + file );