How to get all files from assets folder

20,557

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.

EDIT: 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();
        }

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.

Share:
20,557
oratis
Author by

oratis

Updated on May 16, 2021

Comments

  • oratis
    oratis about 3 years

    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; 
          }
    

    somehow I am not sure about this expression

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

    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?

    I have resolve this problem by using the code below

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

    in case somebody else want to know

  • oratis
    oratis over 12 years
    well,I used Log.d("tag", "get assets" +it); to log if the files in assets folder has been read and logcat print 12-03 14:01:38.780: DEBUG/tag(495): get assets[] I think there is something wrong with the metod I get those file, any advises? thx
  • Huang
    Huang over 12 years
    Actually, I always use the AssetManager to get access to the file inside the asset folder. I remember that the Uri for files in asset folder is "file:///android_asset/", but I don't use or test it. Also, are your images in a sub-directory like /asset/img?
  • oratis
    oratis over 12 years
    No,the reason I put all files in List<String> from assets folder and check for mime type is coz I didn't want to use a particular folder for images files,well, it's rare to find Chinese in this website,I am from Beijing, do you have a QQ acount, maybe we can talk there,more convinient.
  • Huang
    Huang over 12 years
    Here I find you way may be wrong. See this post stackoverflow.com/questions/4879416/…. You should use the AssetManager to read these files.
  • Huang
    Huang over 12 years
    I just test File img=new File("file:///android_asset/"); Log.i("INFO", ""+img.listFiles().length+""); it throws NullPointer Exception.
  • oratis
    oratis over 12 years
    Mine too, maybe I'll store the files in sdcard first then read it thx,man
  • Huang
    Huang over 12 years
  • oratis
    oratis over 12 years
    Er,somehow I can't connect to the chat server,sorry
  • oratis
    oratis over 12 years
    I googled it and found files = this.getResources().getAssets().list(assetDir); I think that will do it ,testing
  • Huang
    Huang over 12 years
    That's OK. I like to learn on this site. We'll meet again for sure. The AssetManager also has such a funtion to list the files under a dir. Please read the developer document. In most cases, it's a good resource to solve your problem.
  • JCarlosR
    JCarlosR over 7 years
    There is a way to get the folder names that are in the assets folder?
  • JCarlosR
    JCarlosR over 7 years
    Using an empty string it is possible to get the name of the folders in "assets" (but take a care; because it adds 3 additional folders: images, sounds and webkit).