资产中的 BitmapFactory.decodeFile 不起作用 (FileNotFindException)

Chu*_*ulo 3 android android-assets bitmapfactory

decodeFile参数指向资产文件夹内的文件时,我不知道为什么不起作用。

     // 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();
            }
Run Code Online (Sandbox Code Playgroud)

有什么解释吗?

小智 5

file:///android_asset/WebView 使用它来加载资产。加载资产编程方式使用getAssets()它返回一个方法AssetMAnager一个上Context对象,如Activity。该open(filename)会返回InputStream到你的资源文件。以下内容Bitmap将从您的资产文件中创建fileName一个Activity,

BitmapFactory.decodeStream(getAssets().open(fileName))
Run Code Online (Sandbox Code Playgroud)