Android reads all the pictures in the assets directory and displays the method _android

Source: Internet
Author: User

This article illustrates the way Android reads all the pictures in the assets directory and displays them. Share to everyone for your reference. The specific methods are analyzed as follows:

Files in the Assets folder are kept in the original file format, and you need to read the files in Assetmanager as a byte stream.
1. Call Getassets () first in the activity to get the Assetmanager reference.
2. The Assetmanager open (String fileName, int accessmode) method specifies the read file and access mode to get the input stream inputstream.
3. Then, read the file with the InputStream of the open file, and remember Inputstream.close () when it is finished.
4. Call Assetmanager.close () to close Assetmanager.
It should be noted that files from resources and assets can only be read and cannot be written.
Here's a look at the sample code used in the activity:

Copy Code code as follows:
list<map<string, object>> catelist = new arraylist<map<string, object>> ();
string[] list_image = null;
try {
Get the filename of all files in the assets/processedimages/directory so that you can use the following open operation
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 {
The 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 a imageview in this layout
catch (IOException e) {
E.printstacktrace ();
finally {
if (open!= null) {
try {
Open.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}

So all the key words "IV" in the map save the bitmap we read in theory, and we should have noticed that we also saved a picture at the "BG" keyword. But it was obtained by means of r.drawable, and the experiment proved that this method could be read and displayed successfully. Why can't the bitmap read from assets be displayed?

The solution is:

Implements the Viewbinder interface and describes the two resource IDs and bitmap scenarios:

Copy Code code as follows:
Adapter.setviewbinder (new Viewbinder () { 
    & nbsp;        
    @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;& nbsp;  
    }   
 return false; 
    } 
});

That's it.
In another case, we read the contents of the assets file in a non-activity class, and this time we have to pass the context of the caller (the activity Class) and then use Context.getassets in the non-activity class () Way to call on the line.

For a simple example:
We have a homeactivity and then we call Getdata.initdata (Homeactivity.this) inside of it.
The InitData method in the GetData class is definitely defined as this:

Copy Code code as follows:
public void InitData
{
Other codes ...
string[] list_image = null;
try {
Get the filename of all files in the assets/processedimages/directory so that you can use the following open operation
List_image = Context.getassets (). List ("Processedimages");//attention this line
catch (IOException E1)
{
E1.printstacktrace ();
}
Other codes ...
}

Because the Getassets method is a context method, it cannot be used directly in a non-activity class.

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.