如何从资产的文件夹中的所有文件 [英] How to get all files from assets folder

查看:125
本文介绍了如何从资产的文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想文件夹使用codeS低于

I am trying to get all images in my assets folder using the codes below

private List<String> getImage()
      {
        /* 设定目前所在路径 */
        List<String> it=new ArrayList<String>();      
        File f=new File("file:///android_asset/");  
        File[] files=f.listFiles();
        Log.d("tag", "读取asset资源");

        /* 将所有文件存入ArrayList中 */
        for(int i=0;i<files.length;i++)
        {
          File file=files[i];
          if(getImageFile(file.getPath()))
            it.add(file.getPath());
        }
        return it;

      }

      private boolean getImageFile(String fName)
      {
        boolean re;

        /* 取得扩展名 */
        String end=fName.substring(fName.lastIndexOf(".")+1,
                      fName.length()).toLowerCase(); 

        /* 按扩展名的类型决定MimeType */
        if(end.equals("jpg")||end.equals("gif")||end.equals("png")
                ||end.equals("jpeg")||end.equals("bmp"))
        {
          re=true;
        }
        else
        {
          re=false;
        }
        return re; 
      }

不知何故,我不知道这前pression

somehow I am not sure about this expression

File f=new File("file:///android_asset/"); 

好吧,我知道读一个txt文件或资产HTML文件时,文件夹,你可以用这一点,但将图像接受这个呢?

Well, I know that when read a txt file or html file from assets folder you can use this,but would images accept this too?

我必须使用以下

private List<String> getImage() throws IOException
      {
        AssetManager assetManager = getAssets();
        String[] files = assetManager.list("image");   
        List<String> it=Arrays.asList(files);
        return it; 

        }

如果别人想知道

推荐答案

在一般情况下,你可以使用AssetManager在资产的文件夹来管理文件。在活动中,调用<一个href=\"http://file:///F:/Android/docs/reference/android/content/ContextWrapper.html#getAssets%28%29\">getAssets ()方法获得一个AssetManager实例。

In general, you can use the AssetManager to manage files in your asset folder. In the activity, call getAssets () method to get an AssetManager Instance.

编辑:
您可以使用AssetManager阅读这样的图片:

you can use the AssetManager to read images like this:

AssetManager am=this.getAssets();
        try {
            Bitmap bmp=BitmapFactory.decodeStream(am.open("009.gif"));
            chosenImageView.setImageBitmap(bmp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

请注意, BitmapFactory.de codeStream()方法,默认情况下,UI线程上运行,因此,如果图像过大的应用程序将卡住。在这种情况下,你可以改变的采样大小或启动一个新的线程来做到这一点。

Note that the BitmapFactory.decodeStream() method runs on UI thread by default, so the app would get stuck if the image is too large. In that case, you can change the samplesize or start a new thread to do this.

这篇关于如何从资产的文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆