android - Assets 中的 BitmapFactory.decodeFile 不起作用(FileNotFindException)

标签 android bitmapfactory android-assets

我不知道为什么 decodeFileparam 指向 Assets 文件夹中的文件时不起作用。

     // Load images from the file path
    String[] dir = null;
    try {
        dir = GenericMainContext.sharedContext.getAssets().list("drawable");
       // dir Log => [ic.png, ic_info_dark.png, ic_launcher_default.png]
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (String uri : dir){ 
        // do your stuff here
        if (uri!=null) {
            Bitmap bitmap = null;
            try {
                bitmap = BitmapFactory.decodeFile("file:///android_asset/drawable/"+uri);               } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

有什么解释吗?

最佳答案

file:///android_asset/ 被WebView用来加载assets。要以编程方式加载 Assets ,请使用 getAssets() 方法,该方法返回 Context 对象(例如 Activity)上的 AssetMAnager . open(filename) 会将 InputStream 返回到您的 Assets 文件。以下将从您的 Assets 文件 fileNameActivity 中创建一个 Bitmap

BitmapFactory.decodeStream(getAssets().open(fileName))

关于android - Assets 中的 BitmapFactory.decodeFile 不起作用(FileNotFindException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31294162/

相关文章:

java - 尝试从图库中获取图像时应用程序崩溃。请查看详情

java - BitmapFactory.decodeFile与decodeStream之间的区别

Android:WebView 提高本地 html 文件的加载速度

java - 运行时异常 : Font not found for every font i've tried - Android

Android 通知图标在某些设备上为空白

Android 应用程序在最小化和旋转时崩溃

java - 自定义 ArrayAdapter 的 getView 未被调用

android fragment sqlite查询崩溃

Android 处理多个屏幕...(相对布局或线性布局)?

java - 在 Android 上将 Canvas 保存为位图