Android method to read and display all images in the assets directory

  • 2020-06-12 10:39:58
  • OfStack

This article illustrates how Android reads and displays all images in the assets directory. Share to everybody for everybody reference. The specific analysis is as follows:

The files in the assets folder are in the original file format and need to be read by AssetManager as a byte stream.
1. Call getAssets() inside Activity to get the AssetManager reference.
2. With AssetManager's open(String fileName, int accessMode) method, the input stream InputStream can be obtained by specifying the read file and the access mode.
3. Then read the file using inputStream with open file, and remember inputStream.close () when finished.
4. Call ES26en.close () to close AssetManager.
Note that files from Resources and Assets can only be read and not written.
Take a look at the sample code used in Activity:

List<Map<String, Object>> cateList = new ArrayList<Map<String, Object>>();  
String[] list_image = null; 
try { 
// get assets/processedimages/ The file name of all the files in the directory for later use when opening operations  
    list_image = context.getAssets().list("processedimages"); 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 

for(int i=0;i<list_image.length;++i) 

    InputStream open = null; 
    try { 
  String temp = "processedimages/"+list_image[i]; 
  open = context.getAssets().open(temp); 
  Bitmap bitmap = BitmapFactory.decodeStream(open); 
  Map<String, Object> map = new HashMap<String, Object>(); 
  map.put("name", list_image[i]); 
  map.put("iv", bitmap); 
  map.put("bg", R.drawable.phone_vip_yes); 
  map.put("cate_id",i); 
  cateList.add(map); 
  // Assign the bitmap to an ImageView in this layout 
    } catch (IOException e) { 
  e.printStackTrace(); 
      } finally { 
  if (open != null) { 
    try { 
      open.close(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
       } 
}

In this way, the keyword "iv" in all map will theoretically save the bitmap we read, but the result is not so. You should notice that we also save an image in the keyword" bg ", but it is obtained by R.drawable.Experiment shows that this method can be successfully read and displayed. Why can't bitmap read from assets display ?

The solution:

Implement the ViewBinder interface and explain the situation of the two resources id and bitmap:

adapter.setViewBinder(new ViewBinder() {  
             
    @Override 
    public boolean setViewValue( 
     View view, 
     Object data, 
     String textRepresentation) { 
 // TODO Auto-generated method stub 
  if((view instanceof ImageView) && (data instanceof Bitmap)) {   
  ImageView imageView = (ImageView) view;   
  Bitmap bmp = (Bitmap) data;   
  imageView.setImageBitmap(bmp);   
  return true;   
     }   
 return false; 
    } 
});

That will do.
In another case, we read the contents of the assets file in the non-ES56en class. At this time, we have to pass the context of the caller (Activity class) and then call it in context.getAssets () in the non-ES60en class.

Here's a simple example:
We have one HomeActivity, and we call GetData.initdata (HomeActivity.this) in it.
The initdata method in the GetData class must be defined as follows:

public void initdata(Context context)
{
//other codes...
      String[] list_image = null;
     try {
     // get assets/processedimages/ The file name of all the files in the directory for later use when opening operations
                list_image = context.getAssets().list("processedimages");//attention this line
          } catch (IOException e1)
            {
                e1.printStackTrace();
            }
      //other codes.....
}

Because the getAssets method is a method under Context, it cannot be used directly in a non-ES80en class.

I hope this article has been helpful to your Android programming.


Related articles: